33using System . Collections . Generic ;
44using System . Data ;
55using System . Data . SqlClient ;
6- using System . Data . SQLite ;
6+ // using System.Data.SQLite;
77using System . Diagnostics ;
88using System . IO ;
99using System . Linq ;
@@ -14,176 +14,176 @@ namespace ROMTest
1414{
1515 public static class DBCURD
1616 {
17- public static string ConnectionCommand = "Data Source={0}.sqlite;Version=3;" ;
18- private static string _MyDatabaseName = "db" ;
19- private static SQLiteConnection _SQLiteConnection ;
20- public static string CreateUserTable { get ; set ; } = @"CREATE TABLE [Users](
21- [UserID][int] IDENTITY(1,1) NOT NULL,
22- [UserName] [varchar] (50) NULL,
23- [Email] [varchar] (100) NULL,
24- [Address] [varchar] (100) NULL" ;
25-
26- public static string CreateProductTable { get ; set ; } = @"CREATE TABLE [Product](
27- [ProductID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
28- [ProductName] [varchar](220) NULL,
29- [ProductDesc] [varchar](220) NULL,
30- [UserID] [int] NULL,
31- [CreateTime] [datetime] NULL)" ;
32-
33- public static void CreateDatabase ( )
34- {
35- if ( File . Exists ( _MyDatabaseName ) )
36- {
37- Debug . WriteLine ( "database is exist" ) ;
38- }
39- else
40- {
41- SQLiteConnection . CreateFile ( _MyDatabaseName ) ;
42- //SQLiteCommand sQLiteCommand = new SQLiteCommand("sqlite3 db.db", _SQLiteConnection);
43- //sQLiteCommand.ExecuteNonQuery();
44- }
45- }
46-
47- public static void Open ( )
48- {
49- _SQLiteConnection = new SQLiteConnection ( string . Format ( ConnectionCommand , _MyDatabaseName ) ) ;
50- _SQLiteConnection . Open ( ) ;
51- }
52-
53- public static void CreateTable ( )
54- {
55- SQLiteCommand sQLiteCommand = new SQLiteCommand ( CreateProductTable , _SQLiteConnection ) ;
56- sQLiteCommand . ExecuteNonQuery ( ) ;
57- }
58-
59- public static void InsertNew ( )
60- {
61- var result = _SQLiteConnection . Execute ( "Insert into Users values (@UserID,@UserName, @Email, @Address)" ,
62- new { UserID = 0 , UserName = "jack1" , Email = "[email protected] " , Address = "上海1" , } ) ; 63-
64- var result1 = _SQLiteConnection . Execute ( "Insert into Users values (@UserID,@UserName, @Email, @Address)" ,
65- new UserInfo { UserName = "jack2" , Email = "[email protected] " , Address = "上海2" , UserID = 2 , } ) ; 66- }
67-
68- public static void InsertNewBulk ( )
69- {
70- var userList = Enumerable . Range ( 0 , 10 ) . Select ( i => new UserInfo
71- {
72- UserID = 10 + i ,
73- UserName = i . ToString ( ) + "userName" ,
74- Address = i . ToString ( ) + "address" ,
75- Email = i . ToString ( ) + "email"
76- } ) ;
77- var result = _SQLiteConnection . Execute ( "INSERT INTO [Users] VALUES (@UserID,@UserName, @Email, @Address)" , userList ) ;
78- }
79-
80- public static void Query ( )
81- {
82- var query = _SQLiteConnection . Query < UserInfo > ( "select * from Users where UserName=@UserName" , new { UserName = "jack" } ) ;
83- }
84-
85- public static void QueryBulk ( )
86- {
87- var sql = "SELECT * FROM [Users] WHERE Email IN @emails" ;
88- var info = _SQLiteConnection . Query < UserInfo > ( sql , new { emails = new string [ 2 ] { "res" , "ress" } } ) ;
89- var infolist = info . ToList ( ) ;
90- }
91-
92- public static void Update ( )
93- {
94- var result = _SQLiteConnection . Execute ( "UPDATE [Users] SET [UserName]='marry' WHERE [UserID]=@UserID" , new UserInfo ( ) { UserID = 11 } ) ;
95- }
96-
97- public static void UpdateBulk ( )
98- {
99- var userList = Enumerable . Range ( 3 , 10 ) . Select ( i => new UserInfo
100- {
101- UserID = 10 + i
102- } ) ;
103- var result = _SQLiteConnection . Execute ( "UPDATE [Users] SET [UserName]='jay' WHERE [UserID]=@UserID" , userList ) ;
104- }
105-
106- public static void Delete ( )
107- {
108- var result = _SQLiteConnection . Execute ( "DELETE FROM [Users] WHERE [UserID]=@UserID" , new UserInfo ( ) { UserID = 10 } ) ;
109- }
110-
111- public static void DeleteBulk ( )
112- {
113- var userList = Enumerable . Range ( 0 , 5 ) . Select ( i => new UserInfo
114- {
115- UserID = i
116- } ) ;
117- var result = _SQLiteConnection . Execute ( "DELETE FROM [Users] WHERE [UserID]=@UserID" , userList ) ;
118- }
119-
120- public static void MultiQuery ( )
121- {
122- var query1 = "SELECT * FROM [Users]" ;
123- var query2 = "SELECT * FROM [Product]" ;
124- var multiQuery = query1 + ";" + query2 ;
125- var multiReader = _SQLiteConnection . QueryMultiple ( multiQuery ) ;
126- var product = multiReader . Read < ProductInfo > ( ) ;
127- var users = multiReader . Read < UserInfo > ( ) ;
128- multiReader . Dispose ( ) ;
129- }
130-
131- public static void OpenTransaction ( )
132- {
133- IDbTransaction dbTransaction = _SQLiteConnection . BeginTransaction ( ) ;
134- var result = _SQLiteConnection . Execute ( "Insert into Users values (@UserID,@UserName, @Email, @Address)" ,
135- new { UserID = 34563 , UserName = "jack1" , Email = "[email protected] " , Address = "上海1" , } ) ; 136-
137- var result1 = _SQLiteConnection . Execute ( "Insert into Users values (@UserID,@UserName, @Email, @Address)" ,
138- new UserInfo { UserID = 33645 , UserName = "jack2" , Email = "[email protected] " , Address = "上海2" , } ) ; 139- var result2 = _SQLiteConnection . Execute ( "DELETE FROM [Product]" ) ;
140- dbTransaction . Commit ( ) ;
141- }
142-
143-
144- public static void IsRecordExist ( )
145- {
146- int id = 13 ;
147- var exists = _SQLiteConnection . ExecuteScalar < bool > ( "SELECT COUNT(1) FROM [Users] WHERE UserID=@id" , new { id } ) ;
148- }
149-
150- public static void InsertOrUpdate ( )
151- {
152- var userList = Enumerable . Range ( 0 , 10 ) . Select ( i => new UserInfo
153- {
154- UserID = i ,
155- Email = i . ToString ( ) + "new email"
156- } ) ;
157- var res = _SQLiteConnection . Execute ( "INSERT OR REPLACE INTO [Users] VALUES (@UserID,@UserName, @Email, @Address)" , userList ) ;
158- }
159-
160- public static void InsertOrIgnore ( )
161- {
162- var productList = Enumerable . Range ( 0 , 10 ) . Select ( i => new ProductInfo1
163- {
164- UserID = i % 2 ,
165- ProductID = i ,
166- ProductName = "old Name" + i . ToString ( ) ,
167- } ) ;
168- var res = _SQLiteConnection . Execute ( "INSERT OR IGNORE INTO [Product] ([UserID],[ProductID],[ProductName]) VALUES (@UserID,@ProductID,@ProductName); UPDATE [Product] SET [ProductName]='update address' WHERE [UserID]=@UserID AND [ProductID]=@ProductID" , productList ) ;
169- }
170-
171- public static void InsertOrReplace ( )
172- {
173- var productList = Enumerable . Range ( 6 , 10 ) . Select ( i => new ProductInfo1
174- {
175- UserID = i % 2 ,
176- ProductID = i ,
177- ProductName = "abc Name" + i . ToString ( ) ,
178- } ) ;
179- int a = productList . ToArray ( ) . Length ;
180- var res = _SQLiteConnection . Execute ( $ "INSERT OR REPLACE INTO [Product] ([UserID],[ProductID],[ProductName],[ProductDesc],[CreateTime]) VALUES(" +
181- $ "@UserID," +
182- $ "@ProductID," +
183- $ "@ProductName," +
184- $ "@ProductDesc," +
185- $ "(SELECT [CreateTime] FROM [Product] WHERE [UserID]=@UserID AND [ProductID]=@ProductID))", productList ) ;
186- }
17+ // public static string ConnectionCommand = "Data Source={0}.sqlite;Version=3;";
18+ // private static string _MyDatabaseName = "db";
19+ //// private static SQLiteConnection _SQLiteConnection;
20+ // public static string CreateUserTable { get; set; } = @"CREATE TABLE [Users](
21+ // [UserID][int] IDENTITY(1,1) NOT NULL,
22+ // [UserName] [varchar] (50) NULL,
23+ // [Email] [varchar] (100) NULL,
24+ // [Address] [varchar] (100) NULL";
25+
26+ // public static string CreateProductTable { get; set; } = @"CREATE TABLE [Product](
27+ // [ProductID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
28+ // [ProductName] [varchar](220) NULL,
29+ // [ProductDesc] [varchar](220) NULL,
30+ // [UserID] [int] NULL,
31+ // [CreateTime] [datetime] NULL)";
32+
33+ // public static void CreateDatabase()
34+ // {
35+ // if (File.Exists(_MyDatabaseName))
36+ // {
37+ // Debug.WriteLine("database is exist");
38+ // }
39+ // else
40+ // {
41+ // // SQLiteConnection.CreateFile(_MyDatabaseName);
42+ // //SQLiteCommand sQLiteCommand = new SQLiteCommand("sqlite3 db.db", _SQLiteConnection);
43+ // //sQLiteCommand.ExecuteNonQuery();
44+ // }
45+ // }
46+
47+ // public static void Open()
48+ // {
49+ // _SQLiteConnection = new SQLiteConnection(string.Format(ConnectionCommand, _MyDatabaseName));
50+ // _SQLiteConnection.Open();
51+ // }
52+
53+ // public static void CreateTable()
54+ // {
55+ // SQLiteCommand sQLiteCommand = new SQLiteCommand(CreateProductTable, _SQLiteConnection);
56+ // sQLiteCommand.ExecuteNonQuery();
57+ // }
58+
59+ // public static void InsertNew()
60+ // {
61+ // var result = _SQLiteConnection.Execute("Insert into Users values (@UserID,@UserName, @Email, @Address)",
62+ // new { UserID = 0, UserName = "jack1", Email = "[email protected] ", Address = "上海1", }); 63+
64+ // var result1 = _SQLiteConnection.Execute("Insert into Users values (@UserID,@UserName, @Email, @Address)",
65+ // new UserInfo { UserName = "jack2", Email = "[email protected] ", Address = "上海2", UserID = 2, }); 66+ // }
67+
68+ // public static void InsertNewBulk()
69+ // {
70+ // var userList = Enumerable.Range(0, 10).Select(i => new UserInfo
71+ // {
72+ // UserID=10+i,
73+ // UserName=i.ToString()+"userName",
74+ // Address=i.ToString()+"address",
75+ // Email=i.ToString()+"email"
76+ // });
77+ // var result = _SQLiteConnection.Execute("INSERT INTO [Users] VALUES (@UserID,@UserName, @Email, @Address)", userList);
78+ // }
79+
80+ // public static void Query()
81+ // {
82+ // var query = _SQLiteConnection.Query<UserInfo>("select * from Users where UserName=@UserName", new { UserName = "jack" });
83+ // }
84+
85+ // public static void QueryBulk()
86+ // {
87+ // var sql = "SELECT * FROM [Users] WHERE Email IN @emails";
88+ // var info = _SQLiteConnection.Query<UserInfo>(sql, new { emails = new string[2] { "res", "ress" } });
89+ // var infolist = info.ToList();
90+ // }
91+
92+ // public static void Update()
93+ // {
94+ // var result = _SQLiteConnection.Execute("UPDATE [Users] SET [UserName]='marry' WHERE [UserID]=@UserID", new UserInfo() { UserID = 11 });
95+ // }
96+
97+ // public static void UpdateBulk()
98+ // {
99+ // var userList = Enumerable.Range(3, 10).Select(i => new UserInfo
100+ // {
101+ // UserID = 10 + i
102+ // });
103+ // var result = _SQLiteConnection.Execute("UPDATE [Users] SET [UserName]='jay' WHERE [UserID]=@UserID", userList);
104+ // }
105+
106+ // public static void Delete()
107+ // {
108+ // var result = _SQLiteConnection.Execute("DELETE FROM [Users] WHERE [UserID]=@UserID", new UserInfo() { UserID = 10 });
109+ // }
110+
111+ // public static void DeleteBulk()
112+ // {
113+ // var userList = Enumerable.Range(0, 5).Select(i => new UserInfo
114+ // {
115+ // UserID = i
116+ // });
117+ // var result = _SQLiteConnection.Execute("DELETE FROM [Users] WHERE [UserID]=@UserID", userList);
118+ // }
119+
120+ // public static void MultiQuery()
121+ // {
122+ // var query1 = "SELECT * FROM [Users]";
123+ // var query2 = "SELECT * FROM [Product]";
124+ // var multiQuery = query1 + ";" + query2;
125+ // var multiReader = _SQLiteConnection.QueryMultiple(multiQuery);
126+ // var product = multiReader.Read<ProductInfo>();
127+ // var users = multiReader.Read<UserInfo>();
128+ // multiReader.Dispose();
129+ // }
130+
131+ // public static void OpenTransaction()
132+ // {
133+ // IDbTransaction dbTransaction = _SQLiteConnection.BeginTransaction();
134+ // var result = _SQLiteConnection.Execute("Insert into Users values (@UserID,@UserName, @Email, @Address)",
135+ // new { UserID = 34563, UserName = "jack1", Email = "[email protected] ", Address = "上海1", }); 136+
137+ // var result1 = _SQLiteConnection.Execute("Insert into Users values (@UserID,@UserName, @Email, @Address)",
138+ // new UserInfo { UserID = 33645, UserName = "jack2", Email = "[email protected] ", Address = "上海2", }); 139+ // var result2 = _SQLiteConnection.Execute("DELETE FROM [Product]");
140+ // dbTransaction.Commit();
141+ // }
142+
143+
144+ // public static void IsRecordExist()
145+ // {
146+ // int id = 13;
147+ // var exists = _SQLiteConnection.ExecuteScalar<bool>("SELECT COUNT(1) FROM [Users] WHERE UserID=@id", new { id });
148+ // }
149+
150+ // public static void InsertOrUpdate()
151+ // {
152+ // var userList = Enumerable.Range(0, 10).Select(i => new UserInfo
153+ // {
154+ // UserID = i,
155+ // Email = i.ToString() + "new email"
156+ // });
157+ // var res = _SQLiteConnection.Execute("INSERT OR REPLACE INTO [Users] VALUES (@UserID,@UserName, @Email, @Address)", userList);
158+ // }
159+
160+ // public static void InsertOrIgnore()
161+ // {
162+ // var productList = Enumerable.Range(0, 10).Select(i => new ProductInfo1
163+ // {
164+ // UserID = i%2,
165+ // ProductID=i,
166+ // ProductName="old Name"+i.ToString(),
167+ // });
168+ // var res = _SQLiteConnection.Execute("INSERT OR IGNORE INTO [Product] ([UserID],[ProductID],[ProductName]) VALUES (@UserID,@ProductID,@ProductName); UPDATE [Product] SET [ProductName]='update address' WHERE [UserID]=@UserID AND [ProductID]=@ProductID", productList);
169+ // }
170+
171+ // public static void InsertOrReplace()
172+ // {
173+ // var productList = Enumerable.Range(6, 10).Select(i => new ProductInfo1
174+ // {
175+ // UserID = i%2 ,
176+ // ProductID = i,
177+ // ProductName = "abc Name" + i.ToString(),
178+ // });
179+ // int a = productList.ToArray().Length;
180+ // var res = _SQLiteConnection.Execute($"INSERT OR REPLACE INTO [Product] ([UserID],[ProductID],[ProductName],[ProductDesc],[CreateTime]) VALUES(" +
181+ // $"@UserID," +
182+ // $"@ProductID," +
183+ // $"@ProductName," +
184+ // $"@ProductDesc," +
185+ // $"(SELECT [CreateTime] FROM [Product] WHERE [UserID]=@UserID AND [ProductID]=@ProductID))", productList);
186+ // }
187187 }
188188
189189 public class UserInfo
0 commit comments