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