SQLiteHelper.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /**************************************
  2. * 作用:SQLLite Server操作实现
  3. * 作者:Nick.Yan
  4. * 日期: 2009-03-29
  5. * 网址:www.redglove.com.cn
  6. * 源码:http://www.51aspx.com/CV/SQLLiteDemo
  7. **************************************/
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Specialized;
  11. using System.Data;
  12. using System.Data.SQLite;
  13. using System.Configuration;
  14. using System.Collections.Generic;
  15. namespace SiteCore
  16. {
  17. public class SQLiteHelper
  18. {
  19. public static string connectionString = "";//Data Source="+ webConfig.sqllite+"; UTF8Encoding=true
  20. //public static string connectionString = "Data Source=" + Application.StartupPath + "/SqlFile/Csbq.s3db;UTF8Encoding=true;";
  21. public SQLiteHelper() { }
  22. #region 公用方法
  23. public static int GetMaxID(string FieldName, string TableName)
  24. {
  25. string strsql = "select max(" + FieldName + ")+1 from " + TableName;
  26. object obj = GetSingle(strsql);
  27. if (obj == null)
  28. {
  29. return 1;
  30. }
  31. else
  32. {
  33. return int.Parse(obj.ToString());
  34. }
  35. }
  36. public static bool Exists(string strSql)
  37. {
  38. object obj = GetSingle(strSql);
  39. int cmdresult;
  40. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  41. {
  42. cmdresult = 0;
  43. }
  44. else
  45. {
  46. cmdresult = int.Parse(obj.ToString());
  47. }
  48. if (cmdresult == 0)
  49. {
  50. return false;
  51. }
  52. else
  53. {
  54. return true;
  55. }
  56. }
  57. public static bool Exists(string strSql, params SQLiteParameter[] cmdParms)
  58. {
  59. object obj = GetSingle(strSql, cmdParms);
  60. int cmdresult;
  61. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  62. {
  63. cmdresult = 0;
  64. }
  65. else
  66. {
  67. cmdresult = int.Parse(obj.ToString());
  68. }
  69. if (cmdresult == 0)
  70. {
  71. return false;
  72. }
  73. else
  74. {
  75. return true;
  76. }
  77. }
  78. #endregion
  79. #region 执行简单SQL语句
  80. /// <summary>
  81. /// 执行SQL语句,返回影响的记录数
  82. /// </summary>
  83. /// <param name="SQLString">SQL语句</param>
  84. /// <returns>影响的记录数</returns>
  85. public static int TestExecuteSql(string SQLString)
  86. {
  87. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  88. {
  89. using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  90. {
  91. try
  92. {
  93. connection.Open();
  94. int rows = cmd.ExecuteNonQuery();
  95. return rows;
  96. }
  97. catch (System.Data.SQLite.SQLiteException E)
  98. {
  99. connection.Close();
  100. throw new Exception(E.Message);
  101. }
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// 执行SQL语句,返回影响的记录数
  107. /// </summary>
  108. /// <param name="SQLString">SQL语句</param>
  109. /// <returns>影响的记录数</returns>
  110. public static int ExecuteSql(string SQLString)
  111. {
  112. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  113. {
  114. using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  115. {
  116. connection.Open();
  117. SQLiteTransaction myTrans = connection.BeginTransaction(IsolationLevel.Serializable);
  118. try
  119. {
  120. cmd.Transaction = myTrans;
  121. int rows = cmd.ExecuteNonQuery();
  122. myTrans.Commit();
  123. return rows;
  124. }
  125. catch (System.Data.SQLite.SQLiteException E)
  126. {
  127. myTrans.Rollback();
  128. connection.Close();
  129. throw new Exception(E.Message);
  130. }
  131. }
  132. }
  133. }
  134. public static int ExecuteSql(string SQLString,string connString)
  135. {
  136. using (SQLiteConnection connection = new SQLiteConnection(connString))
  137. {
  138. using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  139. {
  140. connection.Open();
  141. SQLiteTransaction myTrans = connection.BeginTransaction(IsolationLevel.Serializable);
  142. try
  143. {
  144. cmd.Transaction = myTrans;
  145. int rows = cmd.ExecuteNonQuery();
  146. myTrans.Commit();
  147. return rows;
  148. }
  149. catch (System.Data.SQLite.SQLiteException E)
  150. {
  151. myTrans.Rollback();
  152. connection.Close();
  153. throw new Exception(E.Message);
  154. }
  155. }
  156. }
  157. }
  158. /// <summary>
  159. /// 执行SQL语句,设置命令的执行等待时间
  160. /// </summary>
  161. /// <param name="SQLString"></param>
  162. /// <param name="Times"></param>
  163. /// <returns></returns>
  164. public static int ExecuteSqlByTime(string SQLString, int Times)
  165. {
  166. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  167. {
  168. using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  169. {
  170. try
  171. {
  172. connection.Open();
  173. cmd.CommandTimeout = Times;
  174. int rows = cmd.ExecuteNonQuery();
  175. return rows;
  176. }
  177. catch (System.Data.SQLite.SQLiteException E)
  178. {
  179. connection.Close();
  180. throw new Exception(E.Message);
  181. }
  182. }
  183. }
  184. }
  185. public static void ExecuteSqlTran(List<string> SQLStringList)
  186. {
  187. using (SQLiteConnection conn = new SQLiteConnection(connectionString))
  188. {
  189. conn.Open();
  190. SQLiteCommand cmd = new SQLiteCommand();
  191. cmd.Connection = conn;
  192. SQLiteTransaction tx = conn.BeginTransaction();
  193. cmd.Transaction = tx;
  194. try
  195. {
  196. for (int n = 0; n < SQLStringList.Count; n++)
  197. {
  198. string strsql = SQLStringList[n].ToString();
  199. if (strsql.Trim().Length > 1)
  200. {
  201. cmd.CommandText = strsql;
  202. cmd.ExecuteNonQuery();
  203. }
  204. }
  205. tx.Commit();
  206. }
  207. catch (System.Data.SQLite.SQLiteException E)
  208. {
  209. tx.Rollback();
  210. throw new Exception(E.Message);
  211. }
  212. finally
  213. {
  214. cmd.Dispose();
  215. conn.Close();
  216. }
  217. }
  218. }
  219. /// <summary>
  220. /// 执行多条SQL语句,实现数据库事务。
  221. /// </summary>
  222. /// <param name="SQLStringList">多条SQL语句</param>
  223. public static void ExecuteSqlTran(ArrayList SQLStringList)
  224. {
  225. using (SQLiteConnection conn = new SQLiteConnection(connectionString))
  226. {
  227. conn.Open();
  228. SQLiteCommand cmd = new SQLiteCommand();
  229. cmd.Connection = conn;
  230. SQLiteTransaction tx = conn.BeginTransaction();
  231. cmd.Transaction = tx;
  232. try
  233. {
  234. for (int n = 0; n < SQLStringList.Count; n++)
  235. {
  236. string strsql = SQLStringList[n].ToString();
  237. if (strsql.Trim().Length > 1)
  238. {
  239. cmd.CommandText = strsql;
  240. cmd.ExecuteNonQuery();
  241. }
  242. }
  243. tx.Commit();
  244. }
  245. catch (System.Data.SQLite.SQLiteException E)
  246. {
  247. tx.Rollback();
  248. throw new Exception(E.Message);
  249. }
  250. finally
  251. {
  252. cmd.Dispose();
  253. conn.Close();
  254. }
  255. }
  256. }
  257. /// <summary>
  258. /// 执行带一个存储过程参数的的SQL语句。
  259. /// </summary>
  260. /// <param name="SQLString">SQL语句</param>
  261. /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
  262. /// <returns>影响的记录数</returns>
  263. //public static int ExecuteSql(string SQLString, string content)
  264. //{
  265. // using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  266. // {
  267. // SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
  268. // SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
  269. // myParameter.Value = content;
  270. // cmd.Parameters.Add(myParameter);
  271. // try
  272. // {
  273. // connection.Open();
  274. // int rows = cmd.ExecuteNonQuery();
  275. // return rows;
  276. // }
  277. // catch (System.Data.SQLite.SQLiteException E)
  278. // {
  279. // throw new Exception(E.Message);
  280. // }
  281. // finally
  282. // {
  283. // cmd.Dispose();
  284. // connection.Close();
  285. // }
  286. // }
  287. //}
  288. /// <summary>
  289. /// 执行带一个存储过程参数的的SQL语句。
  290. /// </summary>
  291. /// <param name="SQLString">SQL语句</param>
  292. /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
  293. /// <returns>影响的记录数</returns>
  294. public static object ExecuteSqlGet(string SQLString, string content)
  295. {
  296. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  297. {
  298. SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
  299. SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
  300. myParameter.Value = content;
  301. cmd.Parameters.Add(myParameter);
  302. try
  303. {
  304. connection.Open();
  305. object obj = cmd.ExecuteScalar();
  306. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  307. {
  308. return null;
  309. }
  310. else
  311. {
  312. return obj;
  313. }
  314. }
  315. catch (System.Data.SQLite.SQLiteException E)
  316. {
  317. throw new Exception(E.Message);//发送出错的信息至服务器
  318. }
  319. finally
  320. {
  321. cmd.Dispose();
  322. connection.Close();
  323. }
  324. }
  325. }
  326. /// <summary>
  327. /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
  328. /// </summary>
  329. /// <param name="strSQL">SQL语句</param>
  330. /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
  331. /// <returns>影响的记录数</returns>
  332. public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
  333. {
  334. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  335. {
  336. SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
  337. SQLiteParameter myParameter = new SQLiteParameter("@fs", DbType.Binary);
  338. myParameter.Value = fs;
  339. cmd.Parameters.Add(myParameter);
  340. try
  341. {
  342. connection.Open();
  343. int rows = cmd.ExecuteNonQuery();
  344. return rows;
  345. }
  346. catch (System.Data.SQLite.SQLiteException E)
  347. {
  348. throw new Exception(E.Message);
  349. }
  350. finally
  351. {
  352. cmd.Dispose();
  353. connection.Close();
  354. }
  355. }
  356. }
  357. /// <summary>
  358. /// 执行一条计算查询结果语句,返回查询结果(object)。
  359. /// </summary>
  360. /// <param name="SQLString">计算查询结果语句</param>
  361. /// <returns>查询结果(object)</returns>
  362. public static object GetSingle(string SQLString)
  363. {
  364. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  365. {
  366. using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  367. {
  368. try
  369. {
  370. connection.Open();
  371. object obj = cmd.ExecuteScalar();
  372. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  373. {
  374. return null;
  375. }
  376. else
  377. {
  378. return obj;
  379. }
  380. }
  381. catch (System.Data.SQLite.SQLiteException e)
  382. {
  383. connection.Close();
  384. throw new Exception(e.Message);
  385. }
  386. }
  387. }
  388. }
  389. /// <summary>
  390. /// 执行查询语句,返回SQLiteDataReader(使用该方法切记要手工关闭SQLiteDataReader和连接)
  391. /// </summary>
  392. /// <param name="strSQL">查询语句</param>
  393. /// <returns>SQLiteDataReader</returns>
  394. public static SQLiteDataReader ExecuteReader(string strSQL)
  395. {
  396. SQLiteConnection connection = new SQLiteConnection(connectionString);
  397. SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
  398. try
  399. {
  400. connection.Open();
  401. SQLiteDataReader myReader = cmd.ExecuteReader();
  402. return myReader;
  403. }
  404. catch (System.Data.SQLite.SQLiteException e)
  405. {
  406. throw new Exception(e.Message);//发送出错的信息至服务器
  407. }
  408. //finally //不能在此关闭,否则,返回的对象将无法使用
  409. //{
  410. // cmd.Dispose();
  411. // connection.Close();
  412. //}
  413. }
  414. /// <summary>
  415. /// 执行查询语句,返回DataSet
  416. /// </summary>
  417. /// <param name="SQLString">查询语句</param>
  418. /// <returns>DataSet</returns>
  419. public static DataSet Query(string SQLString)
  420. {
  421. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  422. {
  423. DataSet ds = new DataSet();
  424. try
  425. {
  426. connection.Open();
  427. SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
  428. command.Fill(ds, "ds");
  429. }
  430. catch (System.Data.SQLite.SQLiteException ex)
  431. {
  432. }
  433. return ds;
  434. }
  435. }
  436. public static DataSet Query(string SQLString, string TableName)
  437. {
  438. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  439. {
  440. DataSet ds = new DataSet();
  441. try
  442. {
  443. connection.Open();
  444. SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
  445. command.Fill(ds, TableName);
  446. }
  447. catch (System.Data.SQLite.SQLiteException ex)
  448. {
  449. }
  450. return ds;
  451. }
  452. }
  453. /// <summary>
  454. /// 执行查询语句,返回DataSet,设置命令的执行等待时间
  455. /// </summary>
  456. /// <param name="SQLString"></param>
  457. /// <param name="Times"></param>
  458. /// <returns></returns>
  459. public static DataSet Query(string SQLString, int Times)
  460. {
  461. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  462. {
  463. DataSet ds = new DataSet();
  464. try
  465. {
  466. connection.Open();
  467. SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
  468. command.SelectCommand.CommandTimeout = Times;
  469. command.Fill(ds, "ds");
  470. }
  471. catch (System.Data.SQLite.SQLiteException ex)
  472. {
  473. }
  474. return ds;
  475. }
  476. }
  477. #endregion
  478. #region 执行带参数的SQL语句
  479. /// <summary>
  480. /// 执行SQL语句,返回影响的记录数
  481. /// </summary>
  482. /// <param name="SQLString">SQL语句</param>
  483. /// <returns>影响的记录数</returns>
  484. public static int ExecuteSql(string SQLString, params SQLiteParameter[] cmdParms)
  485. {
  486. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  487. {
  488. using (SQLiteCommand cmd = new SQLiteCommand())
  489. {
  490. try
  491. {
  492. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  493. int rows = cmd.ExecuteNonQuery();
  494. cmd.Parameters.Clear();
  495. return rows;
  496. }
  497. catch (System.Data.SQLite.SQLiteException E)
  498. {
  499. throw new Exception(E.Message);
  500. }
  501. }
  502. }
  503. }
  504. /// <summary>
  505. /// 执行多条SQL语句,实现数据库事务。
  506. /// </summary>
  507. /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SQLiteParameter[])</param>
  508. public static void ExecuteSqlTran(Hashtable SQLStringList)
  509. {
  510. using (SQLiteConnection conn = new SQLiteConnection(connectionString))
  511. {
  512. conn.Open();
  513. using (SQLiteTransaction trans = conn.BeginTransaction())
  514. {
  515. SQLiteCommand cmd = new SQLiteCommand();
  516. try
  517. {
  518. //循环
  519. foreach (DictionaryEntry myDE in SQLStringList)
  520. {
  521. string cmdText = myDE.Key.ToString();
  522. SQLiteParameter[] cmdParms = (SQLiteParameter[])myDE.Value;
  523. PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  524. int val = cmd.ExecuteNonQuery();
  525. cmd.Parameters.Clear();
  526. trans.Commit();
  527. }
  528. }
  529. catch
  530. {
  531. trans.Rollback();
  532. throw;
  533. }
  534. }
  535. }
  536. }
  537. /// <summary>
  538. /// 执行一条计算查询结果语句,返回查询结果(object)。
  539. /// </summary>
  540. /// <param name="SQLString">计算查询结果语句</param>
  541. /// <returns>查询结果(object)</returns>
  542. public static object GetSingle(string SQLString, params SQLiteParameter[] cmdParms)
  543. {
  544. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  545. {
  546. using (SQLiteCommand cmd = new SQLiteCommand())
  547. {
  548. try
  549. {
  550. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  551. object obj = cmd.ExecuteScalar();
  552. cmd.Parameters.Clear();
  553. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  554. {
  555. return null;
  556. }
  557. else
  558. {
  559. return obj;
  560. }
  561. }
  562. catch (System.Data.SQLite.SQLiteException e)
  563. {
  564. throw new Exception(e.Message);
  565. }
  566. }
  567. }
  568. }
  569. /// <summary>
  570. /// 执行查询语句,返回SQLiteDataReader (使用该方法切记要手工关闭SQLiteDataReader和连接)
  571. /// </summary>
  572. /// <param name="strSQL">查询语句</param>
  573. /// <returns>SQLiteDataReader</returns>
  574. public static SQLiteDataReader ExecuteReader(string SQLString, params SQLiteParameter[] cmdParms)
  575. {
  576. SQLiteConnection connection = new SQLiteConnection(connectionString);
  577. SQLiteCommand cmd = new SQLiteCommand();
  578. try
  579. {
  580. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  581. SQLiteDataReader myReader = cmd.ExecuteReader();
  582. cmd.Parameters.Clear();
  583. return myReader;
  584. }
  585. catch (System.Data.SQLite.SQLiteException e)
  586. {
  587. throw new Exception(e.Message);//发送出错的信息至服务器
  588. }
  589. //finally //不能在此关闭,否则,返回的对象将无法使用
  590. //{
  591. // cmd.Dispose();
  592. // connection.Close();
  593. //}
  594. }
  595. /// <summary>
  596. /// 执行查询语句,返回DataSet
  597. /// </summary>
  598. /// <param name="SQLString">查询语句</param>
  599. /// <returns>DataSet</returns>
  600. public static DataSet Query(string SQLString, params SQLiteParameter[] cmdParms)
  601. {
  602. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  603. {
  604. SQLiteCommand cmd = new SQLiteCommand();
  605. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  606. using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd))
  607. {
  608. DataSet ds = new DataSet();
  609. try
  610. {
  611. da.Fill(ds, "ds");
  612. cmd.Parameters.Clear();
  613. }
  614. catch (System.Data.SQLite.SQLiteException ex)
  615. {
  616. throw new Exception(ex.Message);//发送出错的信息至服务器
  617. }
  618. return ds;
  619. }
  620. }
  621. }
  622. public static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText, SQLiteParameter[] cmdParms)
  623. {
  624. if (conn.State != ConnectionState.Open)
  625. conn.Open();
  626. cmd.Connection = conn;
  627. cmd.CommandText = cmdText;
  628. if (trans != null)
  629. cmd.Transaction = trans;
  630. cmd.CommandType = CommandType.Text;//cmdType;
  631. if (cmdParms != null)
  632. {
  633. foreach (SQLiteParameter parameter in cmdParms)
  634. {
  635. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  636. (parameter.Value == null))
  637. {
  638. parameter.Value = DBNull.Value;
  639. }
  640. cmd.Parameters.Add(parameter);
  641. }
  642. }
  643. }
  644. #endregion
  645. #region 参数转换
  646. /// <summary>
  647. /// 放回一个SQLiteParameter
  648. /// </summary>
  649. /// <param name="name">参数名字</param>
  650. /// <param name="type">参数类型</param>
  651. /// <param name="size">参数大小</param>
  652. /// <param name="value">参数值</param>
  653. /// <returns>SQLiteParameter的值</returns>
  654. public static SQLiteParameter MakeSQLiteParameter(string name, DbType type, int size, object value)
  655. {
  656. SQLiteParameter parm = new SQLiteParameter(name, type, size);
  657. parm.Value = value;
  658. return parm;
  659. }
  660. public static SQLiteParameter MakeSQLiteParameter(string name, DbType type, object value)
  661. {
  662. SQLiteParameter parm = new SQLiteParameter(name, type);
  663. parm.Value = value;
  664. return parm;
  665. }
  666. #endregion
  667. }
  668. }