C# SQLite数据库操作类

52 篇文章 1 订阅

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值