哪位能提供一个sqlite数据库的操作类

dx136 2009-10-24 09:39:15
我引用的是System.Data.SQLite.dll
我希望这个操作类里有
像 public int ExecuteSQL(string strSQL)
public DataTable GetDataTable(string strSQL)
之类的函数,这样操作起来比较方便.
...全文
232 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2009-10-24
  • 打赏
  • 举报
回复
dx136 2009-10-24
  • 打赏
  • 举报
回复
嘿嘿,谢谢了,不过我自己找到了
我这个貌似要比你的好用一点

1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Data.SQLite;
5using System.Data;
6using System.Configuration;
7using System.Web;
8using System.Collections;
9namespace FTchina
10...{
11 public class shujuku
12 ...{
13
14 protected static SQLiteConnection conn = new SQLiteConnection();
15 protected static SQLiteCommand comm = new SQLiteCommand();
16 protected static string connstr = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
17
18 /**//// <summary>
19 /// 打开数据库连接
20 /// </summary>
21 public static void OpenConnection()
22 ...{
23 CheckConnection();
24 }
25 public static bool CheckConnection()
26 ...{
27 if (conn.State == ConnectionState.Closed)
28 ...{
29 try
30 ...{
31 conn.ConnectionString = connstr;
32 comm.Connection = conn;
33 conn.Open();
34 }
35 catch
36 ...{
37
38
39 return false;
40 }
41 }
42 return true;
43 }
44 /**//// <summary>
45 /// 关闭当前数据库连接
46 /// </summary>
47 public static void CloseConnection()
48 ...{
49 if (conn.State == ConnectionState.Open)
50 ...{
51 conn.Close();
52 conn.Dispose();
53 comm.Dispose();
54 }
55 }
56 /**//// <summary>
57 /// 执行Sql查询语句
58 /// </summary>
59 /// <param name="sqlstr">传入的Sql语句</param>
60 public static bool ExecuteSql(string sqlstr)
61 ...{
62 try
63 ...{
64 OpenConnection();
65 comm.CommandType = CommandType.Text;
66 comm.CommandText = sqlstr;
67 comm.ExecuteNonQuery();
68 Copyright.CommonContext.Current.QueryExecutions++;
69
70 return true;
71 }
72 catch (System.Exception er)
73 ...{
74
75 return false;
76 }
77 finally
78 ...{
79 CloseConnection();
80 }
81 }
82
83 /**//// <summary>
84 /// 在事务中处理大量数据
85 /// </summary>
86 /// <param name="sqls"></param>
87 public static void ExecuteSqlTransaction(ArrayList sqls)
88 ...{
89
90 OpenConnection();
91
92 SQLiteTransaction transaction = conn.BeginTransaction();
93 try
94 ...{
95
96 for (int i = 0; i < sqls.Count; i++)
97 ...{
98 comm.CommandType = CommandType.Text;
99 comm.CommandText = sqls[i].ToString();
100
101 comm.ExecuteNonQuery();
102
103 Copyright.CommonContext.Current.QueryExecutions++;
104
105 }
106 transaction.Commit();
107 }
108 catch (System.Exception er)
109 ...{
110
111 transaction.Rollback();
112 }
113 finally
114 ...{
115 CloseConnection();
116 }
117 }
118 /**//// <summary>
119 /// 返回指定Sql语句的DataTable
120 /// </summary>
121 /// <param name="sqlstr">传入的Sql语句</param>
122 /// <returns>DataTable</returns>
123 public static DataTable GetDataTable(string sqlstr)
124 ...{
125 return GetDataSet(sqlstr).Tables[0];
126
127 }
128
129
130 /**//// <summary>
131 /// 获取只包含架构信息的表
132 /// </summary>
133 /// <param name="sqlstr"></param>
134 /// <returns></returns>
135 public static DataTable GetSchemaTable(string sqlstr)
136 ...{
137 DataTable dt = new DataTable();
138 try
139 ...{
140 OpenConnection();
141 comm.CommandType = CommandType.Text;
142 comm.CommandText = sqlstr;
143
144 SQLiteDataReader dr = comm.ExecuteReader(CommandBehavior.SchemaOnly);
145 dt = dr.GetSchemaTable();
146 dr.Close();
147 }
148 catch (System.Exception er)
149 ...{
150
151 }
152 finally ...{ CloseConnection(); }
153 return dt;
154 }
155
156 /**//// <summary>
157 /// 返回指定Sql语句的DataSet
158 /// </summary>
159 /// <param name="sqlstr">传入的Sql语句</param>
160 /// <returns>DataTable</returns>
161 public static DataSet GetDataSet(string sqlstr)
162 ...{
163 SQLiteDataAdapter da = new SQLiteDataAdapter();
164 DataSet dataset = new DataSet();
165 try
166 ...{
167 OpenConnection();
168 comm.CommandType = CommandType.Text;
169 comm.CommandText = sqlstr;
170 da.SelectCommand = comm;
171 da.Fill(dataset);
172 Copyright.CommonContext.Current.QueryExecutions++;
173 }
174 catch (System.Exception er)
175 ...{
176
177 }
178 finally
179 ...{
180 CloseConnection();
181 }
182 return dataset;
183 }
184
185 //检测数据是否存在,返回真假
186 public static bool testread(string sqlstr)
187 ...{
188 return (GetDataSet(sqlstr).Tables[0].Rows.Count > 0);
189 }
190
191 /**//// <summary>
192 /// 返回特定值的查询结果
193 /// </summary>
194 /// <param name="wKey">列名称 如 "select com from table where id=3 "中的com</param>
195 /// <param name="fTable">表名称 如 "select com from table"中的table</param>
196 /// <param name="wStr">限制条件 如 "select com from table where id=3 "中的id=3</param>
197 /// <returns></returns>
198 public static string GetValueByKey(string wKey, string fTable, string wStr)
199 ...{
200 try
201 ...{
202 return GetDataTable("select " + wKey + " from " + fTable + " where " + wStr).Rows[0][0].ToString();
203 }
204 catch (System.Exception er)
205 ...{
206
207 return "";
208 }
209 }
210
211
212 分页的方法page为当前页码,NUMCount为每页数据#region 分页的方法page为当前页码,NUMCount为每页数据
213 public static DataSet ShowForPage(string table, string Where, int curragePage, int NUMCount)
214 ...{
215 int number = 0;
216 if (curragePage > 1)
217 ...{
218 number = NUMCount * (curragePage - 1);
219 }
220
221 string strsql = "select * from " + table + " where " + Where + " order by id desc limit " + number + "," + NUMCount;
222 return GetDataSet(strsql);
223 }
224 #endregion
225 }
226}



刚找到的,还没测试.
zzxap 2009-10-24
  • 打赏
  • 举报
回复
http://www.51aspx.com/Tags/1142/
PandaIT 2009-10-24
  • 打赏
  • 举报
回复

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧