SQLiteHelper1.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SQLite;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. namespace MeterVision.db
  10. {
  11. public abstract class SQLiteHelper1
  12. {
  13. private static string DBFileName = AppDomain.CurrentDomain.BaseDirectory + "\\MeterVision.db";
  14. private static string connectionString = string.Format("data source={0}", DBFileName);
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. public SQLiteHelper1()
  19. {
  20. }
  21. /// <summary>
  22. /// 获取连接字符串
  23. /// </summary>
  24. /// <returns>返回连接字符串</returns>
  25. public static string GetConnStr()
  26. {
  27. return SQLiteHelper1.connectionString;
  28. }
  29. /// <summary>
  30. /// 创建本地库连接
  31. /// </summary>
  32. /// <returns>返回本地库连接</returns>
  33. public static SQLiteConnection CreatConn()
  34. {
  35. SQLiteConnection connection = new SQLiteConnection(connectionString);
  36. return connection;
  37. }
  38. /// <summary>
  39. /// 从SQL语句中返回一个值
  40. /// </summary>
  41. /// <param name="SQLString">SQL语句</param>
  42. /// <returns>返回的值</returns>
  43. public static int ExecuteSel(string SQLString)
  44. {
  45. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  46. {
  47. using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  48. {
  49. try
  50. {
  51. connection.Open();
  52. int count = Convert.ToInt32(cmd.ExecuteScalar());
  53. return count;
  54. }
  55. catch (SQLiteException e)
  56. {
  57. connection.Close();
  58. throw e;
  59. }
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// 执行SQL语句,返回影响的记录数
  65. /// </summary>
  66. /// <param name="SQLString">SQL语句</param>
  67. /// <returns>影响的记录数</returns>
  68. public static int ExecuteSql(string SQLString)
  69. {
  70. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  71. {
  72. using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  73. {
  74. try
  75. {
  76. connection.Open();
  77. int rows = cmd.ExecuteNonQuery();
  78. return rows;
  79. }
  80. catch (SQLiteException e)
  81. {
  82. connection.Close();
  83. throw e;
  84. }
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// 执行一组SQL语句
  90. /// </summary>
  91. /// <param name="ColSQL">SQL语句集合</param>
  92. /// <returns>成功返回True,失败返回False</returns>
  93. public static bool ExecuteSql(List<string> ColSQL)
  94. {
  95. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  96. {
  97. connection.Open();
  98. SQLiteTransaction transaction;
  99. transaction = connection.BeginTransaction();
  100. try
  101. {
  102. //connection.Open();
  103. foreach (string SQLString in ColSQL)
  104. {
  105. using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  106. {
  107. cmd.ExecuteNonQuery();
  108. }
  109. }
  110. transaction.Commit();
  111. return true;
  112. }
  113. catch (SQLiteException e)
  114. {
  115. transaction.Rollback();
  116. //return false;
  117. throw e;
  118. }
  119. finally
  120. {
  121. connection.Close();
  122. transaction.Dispose();
  123. transaction = null;
  124. }
  125. }
  126. }
  127. /// <summary>
  128. ///
  129. /// </summary>
  130. /// <param name="SQLString"></param>
  131. /// <param name="cmdParms"></param>
  132. /// <returns></returns>
  133. public static int ExecuteSql(string SQLString, SQLiteParameter[] cmdParms)
  134. {
  135. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  136. {
  137. using (SQLiteCommand cmd = new SQLiteCommand())
  138. {
  139. try
  140. {
  141. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  142. int rows = cmd.ExecuteNonQuery();
  143. cmd.Parameters.Clear();
  144. return rows;
  145. }
  146. catch (SQLiteException e)
  147. {
  148. throw e;
  149. }
  150. }
  151. }
  152. }
  153. private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText, SQLiteParameter[] cmdParms)
  154. {
  155. if (conn.State != ConnectionState.Open)
  156. conn.Open();
  157. cmd.Connection = conn;
  158. cmd.CommandText = cmdText;
  159. if (trans != null)
  160. cmd.Transaction = trans;
  161. cmd.CommandType = CommandType.Text;//cmdType;
  162. if (cmdParms != null)
  163. {
  164. foreach (SQLiteParameter parameter in cmdParms)
  165. {
  166. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  167. (parameter.Value == null))
  168. {
  169. parameter.Value = DBNull.Value;
  170. }
  171. cmd.Parameters.Add(parameter);
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// 删除指定表的所有数据
  177. /// </summary>
  178. /// <param name="TableName">指定的表名</param>
  179. /// <returns>影响的记录输</returns>
  180. public static int DelTableData(string TableName)
  181. {
  182. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  183. {
  184. string SQLString = string.Format("delete from '{0}'", TableName);
  185. using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  186. {
  187. try
  188. {
  189. connection.Open();
  190. int rows = cmd.ExecuteNonQuery();
  191. return rows;
  192. }
  193. catch (SQLiteException e)
  194. {
  195. connection.Close();
  196. throw e;
  197. }
  198. }
  199. }
  200. }
  201. /// <summary>
  202. /// 执行一条计算查询结果语句,返回查询结果(object)。
  203. /// </summary>
  204. /// <param name="SQLString">计算查询结果语句</param>
  205. /// <returns>查询结果(object)</returns>
  206. public static object GetSingle(string SQLString)
  207. {
  208. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  209. {
  210. using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  211. {
  212. try
  213. {
  214. connection.Open();
  215. object obj = cmd.ExecuteScalar();
  216. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  217. {
  218. return null;
  219. }
  220. else
  221. {
  222. return obj;
  223. }
  224. }
  225. catch (SQLiteException e)
  226. {
  227. connection.Close();
  228. throw e;
  229. }
  230. }
  231. }
  232. }
  233. /// <summary>
  234. /// 执行查询语句,返回DataSet
  235. /// </summary>
  236. /// <param name="SQLString">查询语句</param>
  237. /// <returns>DataSet</returns>
  238. public static DataSet Query(string SQLString)
  239. {
  240. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  241. {
  242. DataSet ds = new DataSet();
  243. try
  244. {
  245. connection.Open();
  246. SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
  247. command.Fill(ds, "ds");
  248. }
  249. catch (SQLiteException ex)
  250. {
  251. throw new Exception(ex.Message);
  252. }
  253. return ds;
  254. }
  255. }
  256. /// <summary>
  257. /// 执行查询语句,返回DataTable
  258. /// </summary>
  259. /// <param name="SQLString">查询语句</param>
  260. /// <returns>DataTable</returns>
  261. public static DataTable QueryTable(string SQLString)
  262. {
  263. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  264. {
  265. DataSet ds = new DataSet();
  266. try
  267. {
  268. connection.Open();
  269. SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
  270. command.Fill(ds, "ds");
  271. }
  272. catch (SQLiteException ex)
  273. {
  274. throw new Exception(ex.Message);
  275. //ex.State =
  276. //connection.Close();
  277. }
  278. return ds.Tables[0];
  279. }
  280. }
  281. /// <summary>
  282. ///
  283. /// </summary>
  284. /// <param name="SQLString"></param>
  285. /// <param name="ds"></param>
  286. /// <param name="sTName"></param>
  287. public static void QueryD(string SQLString, DataSet ds, string sTName)
  288. {
  289. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  290. {
  291. //DataSet ds = new DataSet();
  292. try
  293. {
  294. connection.Open();
  295. SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
  296. //command.Fill(ds, "ds");
  297. command.Fill(ds, sTName);
  298. }
  299. catch (SQLiteException ex)
  300. {
  301. throw new Exception(ex.Message);
  302. //ex.State =
  303. //connection.Close();
  304. }
  305. //return ds.Tables[0];
  306. }
  307. }
  308. /// <summary>
  309. /// 执行查询语句,返回表的空表格式
  310. /// </summary>
  311. /// <param name="sTableName">表名</param>
  312. /// <returns>DataTable</returns>
  313. public static DataTable QueryNullTable(string sTableName)
  314. {
  315. string SQLString = string.Format("select * from {0} Limit 0", sTableName);
  316. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  317. {
  318. DataSet ds = new DataSet();
  319. try
  320. {
  321. connection.Open();
  322. SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
  323. command.Fill(ds, "ds");
  324. }
  325. catch (SQLiteException ex)
  326. {
  327. throw new Exception(ex.Message);
  328. //ex.State =
  329. //connection.Close();
  330. }
  331. return ds.Tables[0];
  332. }
  333. }
  334. /// <summary>
  335. /// 增加新记录
  336. /// </summary>
  337. /// <param name="sTableName"></param>
  338. /// <param name="dr"></param>
  339. /// <returns></returns>
  340. public static bool AddNewRow(string sTableName, DataRow dr)
  341. {
  342. try
  343. {
  344. string SQLString = string.Format("select * from {0} Limit 0", sTableName);
  345. SQLiteDataAdapter da = new SQLiteDataAdapter(SQLString, connectionString);
  346. SQLiteCommandBuilder scb = new SQLiteCommandBuilder(da);
  347. DataRow[] drList = new DataRow[] { dr };
  348. da.Update(drList);
  349. scb.Dispose();
  350. scb = null;
  351. return true;
  352. }
  353. catch
  354. {
  355. return false;
  356. }
  357. //return true;
  358. }
  359. /// <summary>
  360. /// 产生新行
  361. /// </summary>
  362. /// <param name="sTableName"></param>
  363. /// <returns></returns>
  364. public static DataRow CreatNewRow(string sTableName)
  365. {
  366. try
  367. {
  368. string SQLString = string.Format("select * from {0} Limit 0", sTableName);
  369. DataTable dt = QueryTable(SQLString);
  370. DataRow dr = dt.NewRow();
  371. //dt.Dispose();
  372. //dt = null;
  373. return dr;
  374. }
  375. catch (SQLiteException ex)
  376. {
  377. throw ex;
  378. }
  379. }
  380. /// <summary>
  381. /// 获取表的结构
  382. /// </summary>
  383. /// <param name="sTableName">表名</param>
  384. /// <returns></returns>
  385. public static DataTable GetDTFormat(string sTableName)
  386. {
  387. string SQLString = string.Format("select * from {0} Limit 0", sTableName);
  388. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  389. {
  390. DataSet ds = new DataSet();
  391. try
  392. {
  393. connection.Open();
  394. SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
  395. command.Fill(ds, "ds");
  396. }
  397. catch (SQLiteException ex)
  398. {
  399. throw new Exception(ex.Message);
  400. //ex.State =
  401. //connection.Close();
  402. }
  403. return ds.Tables[0];
  404. }
  405. }
  406. /// <summary>
  407. /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
  408. /// </summary>
  409. /// <param name="strSQL">查询语句</param>
  410. /// <returns>SqlDataReader</returns>
  411. public static SQLiteDataReader ExecuteReader(string strSQL)
  412. {
  413. SQLiteConnection connection = new SQLiteConnection(connectionString);
  414. SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
  415. try
  416. {
  417. connection.Open();
  418. SQLiteDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  419. return myReader;
  420. }
  421. catch (SQLiteException e)
  422. {
  423. throw e;
  424. }
  425. }
  426. /// <summary>
  427. /// 执行语句清理多余空间
  428. /// </summary>
  429. public static void ClearSQLite()
  430. {
  431. SQLiteConnection connection = new SQLiteConnection(connectionString);
  432. try
  433. {
  434. using (SQLiteCommand cmd = new SQLiteCommand("VACUUM", connection))
  435. {
  436. connection.Open();
  437. cmd.ExecuteNonQuery();
  438. }
  439. }
  440. catch (InvalidOperationException e)
  441. { }
  442. catch (SQLiteException e)
  443. {
  444. throw e;
  445. }
  446. }
  447. }//--SqlliteApp结束
  448. }