teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

DBアクセスコードを追加

2018/12/21 05:28

投稿

hoge_beginner
hoge_beginner

スコア16

title CHANGED
File without changes
body CHANGED
@@ -22,6 +22,7 @@
22
22
  ReadOnly = true
23
23
  };
24
24
 
25
+ string test = null;
25
26
  using (SQLiteConnection dbConnection = new SQLiteConnection(dbConnectionString.ToString()))
26
27
  {
27
28
  dbConnection.Open();
@@ -29,6 +30,14 @@
29
30
  {
30
31
  dbContext.ObjectTrackingEnabled = false;
31
32
  /* ここでLINQでSQLを構築しデータアクセス */
33
+ var items = from test in dbContext.GetTable<TestTable>()
34
+ where test.Id == 1
35
+ select test;
36
+ foreach (var item in items)
37
+ {
38
+ test = String.Copy(item.TestName);
39
+ break;
40
+ }
32
41
  }
33
42
  }
34
43
  dbConnectionString = null;
@@ -36,4 +45,24 @@
36
45
  timer.Start();
37
46
  Console.ReadKey();
38
47
  }
48
+
49
+ [Table(Name = "T_TEST")]
50
+ public class TestTable
51
+ {
52
+ [Column(Name = "ID", DbType = "BIGINT", CanBeNull = false, IsPrimaryKey = true)]
53
+ public Int64 Id { get; set; }
54
+
55
+ [Column(Name = "TestName", DbType = "NVARCHAR", CanBeNull = false)]
56
+ public string TestName { get; set; }
57
+
58
+ public TestTable()
59
+ {
60
+ this.Id = -1;
61
+ this.TestName = "";
62
+ }
63
+ }
64
+
65
+
39
- ```
66
+ ```
67
+
68
+ 環境:VisualStudio 2013 .Net Framework 4.5

1

DBアクセス部分のコードを追記

2018/12/21 05:28

投稿

hoge_beginner
hoge_beginner

スコア16

title CHANGED
File without changes
body CHANGED
@@ -21,7 +21,16 @@
21
21
  BusyTimeout = 10,
22
22
  ReadOnly = true
23
23
  };
24
+
25
+ using (SQLiteConnection dbConnection = new SQLiteConnection(dbConnectionString.ToString()))
26
+ {
27
+ dbConnection.Open();
28
+ using (DataContext dbContext = new DataContext(dbConnection))
29
+ {
30
+ dbContext.ObjectTrackingEnabled = false;
24
- /* ここでデータベーの読み取り処理 */
31
+ /* ここでLINQでSQLを構築しデータアクセス */
32
+ }
33
+ }
25
34
  dbConnectionString = null;
26
35
  };
27
36
  timer.Start();