C# SQLite数据库操做类

C# SQLite数据库操做类,可创建SQLiteConnection 数据库链接、Query数据库、执行ExcDbCommand SQL命令,以及SQLiteParameter[] 、ExecuteReader()、ExecuteNonQuery()、SQLiteDataAdapter等操做。sql

01 using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Text;
05 using System.Configuration;
06 using System.Data.SQLite;
07 using System.Data;
08 namespace DBHelper.SQLite
09 {
10     public class SQLiteHelper
11     {
12         static string _connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
13         SQLiteConnection _connection;
14  
15         public SQLiteHelper()
16         {
17             _connection = new SQLiteConnection(_connectionString);
18         }
19         public SQLiteHelper(string connectionString)
20             :this()
21         {
22             _connectionString = connectionString;
23         }
24  
25         public void Open()
26         {
27             if (_connection.State != ConnectionState.Open)
28                 _connection.Open();
29         }
30         public void Close()
31         {
32             if (_connection.State != ConnectionState.Closed)
33                 _connection.Close();
34         }
35  
36         public DataSet Query(string sql)
37         {
38             return Query(sql, null);
39         }
40  
41         public DataSet Query(string sql, params SQLiteParameter[] parameters)
42         {
43             //this.Open();
44             SQLiteCommand command = ExcDbCommand(sql, parameters);
45             DataSet ds = new DataSet();
46             SQLiteDataAdapter da = new SQLiteDataAdapter(command);
47             da.Fill(ds);
48             //this.Close();
49             return ds;
50         }
51  
52         public bool Exc(string sql)
53         {
54             return Exc(sql, null);
55         }
56         public bool Exc(string sql, params SQLiteParameter[] parameters)
57         {
58             SQLiteCommand command = ExcDbCommand(sql, parameters);
59             this.Open();
60             int result = command.ExecuteNonQuery();
61             this.Close();
62             return result > 0;
63         }
64         public SQLiteDataReader Read(string sql)
65         {
66             return Read(sql, null);
67         }
68         public SQLiteDataReader Read(string sql, params SQLiteParameter[] parameters)
69         {
70             SQLiteCommand command = ExcDbCommand(sql, parameters);
71             this.Open();
72             SQLiteDataReader reader = command.ExecuteReader();
73             //this.Close();
74             return reader;
75         }
76         SQLiteCommand ExcDbCommand(string sql, SQLiteParameter[] parameters)
77         {
78             SQLiteCommand command = new SQLiteCommand(sql, _connection);
79             command.CommandType = CommandType.Text;
80             if (parameters == null || parameters.Length == 0)
81                 return command;
82             foreach (SQLiteParameter param in parameters)
83             {
84                 if(param != null)
85                     command.Parameters.Add(param);
86             }
87             return command;
88         }
89     }
90 }

以上文件可保存文件名为:SQLiteHelper.cs数据库