C# SQLiteHelper

 1 public class SQLiteHelpers
 2     {
 3         /// <summary>          
 4         /// ConnectionString样例:Datasource=Test.db3;Pooling=true;FailIfMissing=false  
 5         /// </summary>          
 6         public static string ConnectionString { get; set; }
 7         private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params object[] p)
 8         {
 9             if (conn.State != ConnectionState.Open)
10                 conn.Open();
11             cmd.Parameters.Clear();
12             cmd.Connection = conn;
13             cmd.CommandText = cmdText;
14             cmd.CommandType = CommandType.Text;
15             cmd.CommandTimeout = 30;
16             if (p != null)
17             {
18                 foreach (object parm in p)
19                     cmd.Parameters.AddWithValue(string.Empty, parm);
20             }
21         }
22         public static DataSet ExecuteQuery(string cmdText, params object[] p)
23         {
24             using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
25             {
26                 using (SQLiteCommand command = new SQLiteCommand())
27                 {
28                     DataSet ds = new DataSet();
29                     PrepareCommand(command, conn, cmdText, p);
30                     SQLiteDataAdapter da = new SQLiteDataAdapter(command);
31                     da.Fill(ds);
32                     return ds;
33                 }
34             }
35         }
36         public static int ExecuteNonQuery(string cmdText, params object[] p)
37         {
38             using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
39             {
40                 using (SQLiteCommand command = new SQLiteCommand())
41                 {
42                     PrepareCommand(command, conn, cmdText, p); 
43                     return command.ExecuteNonQuery();
44                 }
45             }
46         }
47         public static SQLiteDataReader ExecuteReader(string cmdText, params object[] p)
48         {
49             using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
50             {
51                 using (SQLiteCommand command = new SQLiteCommand())
52                 {
53                     PrepareCommand(command, conn, cmdText, p); 
54                     return command.ExecuteReader(CommandBehavior.CloseConnection);
55                 }
56             }
57         }
58         public static object ExecuteScalar(string cmdText, params object[] p)
59         {
60             using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
61             {
62                 using (SQLiteCommand command = new SQLiteCommand())
63                 {
64                     PrepareCommand(command, conn, cmdText, p); 
65                     return command.ExecuteScalar();
66                 }
67             }
68         }
69     }
70 }

 

转载于:https://www.cnblogs.com/ygd-boke/p/4398389.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值