1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data.SQLite;
  5. using System.Data;
  6. namespace MySQLiteDemo
  7. {
  8. public class SqliteHelper
  9. {
  10. private string _connectString = string.Empty;
  11. public string ConnectString
  12. {
  13. get { return _connectString; }
  14. set { _connectString = value; }
  15. }
  16. /// <summary>
  17. /// 检测
  18. /// </summary>
  19. /// <returns></returns>
  20. public static bool TestLite(string strConn)
  21. {
  22. SQLiteConnection conn = new SQLiteConnection(strConn);
  23. conn.Open();
  24. if (conn.State != ConnectionState.Open)
  25. {
  26. return false;
  27. }
  28. return true;
  29. }
  30. /// <summary>
  31. /// 获取数据
  32. /// </summary>
  33. /// <returns></returns>
  34. public DataTable GetData(string strSql)
  35. {
  36. return GetData(strSql, null);
  37. }
  38. /// <summary>
  39. /// 获取数据
  40. /// </summary>
  41. /// <returns></returns>
  42. public DataTable GetData(string strSql, SQLiteParameter[] cmdParams)
  43. {
  44. using (SQLiteConnection con = new SQLiteConnection(_connectString))
  45. {
  46. try
  47. {
  48. SQLiteCommand cmd = new SQLiteCommand(strSql, con);
  49. if (cmdParams != null)
  50. {
  51. foreach (SQLiteParameter param in cmdParams)
  52. {
  53. cmd.Parameters.Add(param);
  54. }
  55. }
  56. con.Open();
  57. DataTable ret = new DataTable();
  58. SQLiteDataAdapter ad = new SQLiteDataAdapter(cmd);
  59. ad.Fill(ret);
  60. return ret;
  61. }
  62. catch (SQLiteException ex)
  63. {
  64. throw new Exception(ex.Message);
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// 获取SQLiteDataReader
  70. /// </summary>
  71. /// <returns></returns>
  72. public SQLiteDataReader GetReader(string strSql)
  73. {
  74. return GetReader(strSql, null);
  75. }
  76. /// <summary>
  77. /// 执行带参数的查询语句,返回SQLiteDataReader ( 注意:调用该方法后,一定要对SQLiteDataReader进行Close )
  78. /// </summary>
  79. /// <param name="strSQL">查询语句</param>
  80. /// <returns>SqlDataReader</returns>
  81. public SQLiteDataReader GetReader(string strSql, params SQLiteParameter[] cmdParms)
  82. {
  83. using (SQLiteConnection con = new SQLiteConnection(_connectString))
  84. {
  85. try
  86. {
  87. SQLiteCommand cmd = new SQLiteCommand(strSql, con);
  88. if (cmdParms != null)
  89. {
  90. foreach (SQLiteParameter param in cmdParms)
  91. {
  92. cmd.Parameters.Add(param);
  93. }
  94. }
  95. con.Open();
  96. SQLiteDataReader myReader = cmd.ExecuteReader();
  97. cmd.Parameters.Clear();
  98. return myReader;
  99. }
  100. catch (SQLiteException ex)
  101. {
  102. throw new Exception(ex.Message);
  103. }
  104. }
  105. }
  106. /// <summary>
  107. /// 执行语句
  108. /// </summary>
  109. /// <param name="sql"></param>
  110. /// <returns></returns>
  111. public int ExecuteSql(string strSql)
  112. {
  113. using (SQLiteConnection con = new SQLiteConnection(_connectString))
  114. {
  115. try
  116. {
  117. SQLiteCommand cmd = new SQLiteCommand(strSql, con);
  118. con.Open();
  119. return cmd.ExecuteNonQuery();
  120. }
  121. catch (SQLiteException ex)
  122. {
  123. throw new Exception(ex.Message);
  124. }
  125. }
  126. }
  127. /// <summary>
  128. /// 执行语句
  129. /// </summary>
  130. /// <param name="sql"></param>
  131. /// <returns></returns>
  132. public int ExecuteSql(string strSql, SQLiteParameter[] cmdParams)
  133. {
  134. using (SQLiteConnection con = new SQLiteConnection(_connectString))
  135. {
  136. try
  137. {
  138. SQLiteCommand cmd = new SQLiteCommand(strSql, con);
  139. if (cmdParams != null)
  140. {
  141. foreach (SQLiteParameter param in cmdParams)
  142. {
  143. cmd.Parameters.Add(param);
  144. }
  145. }
  146. con.Open();
  147. int rows = cmd.ExecuteNonQuery();
  148. cmd.Parameters.Clear();
  149. return rows;
  150. }
  151. catch (SQLiteException ex)
  152. {
  153. throw new Exception(ex.Message);
  154. }
  155. }
  156. }
  157. /// <summary>
  158. /// 获取当前第一行第一列值
  159. /// </summary>
  160. /// <param name="strsql"></param>
  161. /// <param name="cmdParams"></param>
  162. /// <returns></returns>
  163. public object GetSingle(string strSql)
  164. {
  165. return GetSingle(strSql, null);
  166. }
  167. /// <summary>
  168. /// 获取当前第一行第一列值
  169. /// </summary>
  170. /// <param name="strsql"></param>
  171. /// <param name="cmdParams"></param>
  172. /// <returns></returns>
  173. public object GetSingle(string strSql, SQLiteParameter[] cmdParams)
  174. {
  175. using (SQLiteConnection con = new SQLiteConnection(_connectString))
  176. {
  177. try
  178. {
  179. SQLiteCommand cmd = new SQLiteCommand(strSql, con);
  180. if (cmdParams != null)
  181. {
  182. foreach (SQLiteParameter param in cmdParams)
  183. {
  184. cmd.Parameters.Add(param);
  185. }
  186. }
  187. con.Open();
  188. return cmd.ExecuteScalar();
  189. }
  190. catch (SQLiteException ex)
  191. {
  192. throw new Exception(ex.Message);
  193. }
  194. }
  195. }
  196. /// <summary>
  197. /// 页级操作
  198. /// </summary>
  199. /// <param name="sql"></param>
  200. /// <param name="startRecord"></param>
  201. /// <param name="maxRecord"></param>
  202. /// <returns></returns>
  203. public DataTable FindByPaging(string strSql, SQLiteParameter[] cmdParams, int startRecord, int maxRecord)
  204. {
  205. using (SQLiteConnection con = new SQLiteConnection(_connectString))
  206. {
  207. try
  208. {
  209. SQLiteCommand cmd = new SQLiteCommand(strSql + " LIMIT " + +startRecord + "," + maxRecord, con);
  210. if (cmdParams != null)
  211. {
  212. foreach (SQLiteParameter param in cmdParams)
  213. {
  214. cmd.Parameters.Add(param);
  215. }
  216. }
  217. con.Open();
  218. DataTable ret = new DataTable();
  219. SQLiteDataAdapter ad = new SQLiteDataAdapter(cmd);
  220. ad.Fill(ret);
  221. return ret;
  222. }
  223. catch (SQLiteException ex)
  224. {
  225. throw new Exception(ex.Message);
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// 启用事务.
  231. /// </summary>
  232. /// <param name="objTrans"></param>
  233. /// <returns></returns>
  234. public SQLiteTransaction BeginSQLTrans()
  235. {
  236. SQLiteConnection con = new SQLiteConnection(_connectString);
  237. con.Open();
  238. return con.BeginTransaction();
  239. }
  240. /// <summary>
  241. /// 执行语句
  242. /// </summary>
  243. /// <param name="strSql"></param>
  244. /// <param name="cmdParams"></param>
  245. /// <param name="trans"></param>
  246. /// <returns></returns>
  247. public int ExecuteSql(string strSql, SQLiteTransaction trans)
  248. {
  249. return ExecuteSql(strSql, null, trans);
  250. }
  251. /// <summary>
  252. /// 执行语句
  253. /// </summary>
  254. /// <param name="strSql"></param>
  255. /// <param name="cmdParams"></param>
  256. /// <param name="trans"></param>
  257. /// <returns></returns>
  258. public int ExecuteSql(string strSql, SQLiteParameter[] cmdParams, SQLiteTransaction trans)
  259. {
  260. int ret = 0;
  261. using (SQLiteCommand cmd = new SQLiteCommand(strSql, trans.Connection))
  262. {
  263. try
  264. {
  265. if (cmdParams != null)
  266. {
  267. foreach (SQLiteParameter param in cmdParams)
  268. {
  269. cmd.Parameters.Add(param);
  270. }
  271. }
  272. if (trans != null)
  273. cmd.Transaction = trans;
  274. ret = cmd.ExecuteNonQuery();
  275. }
  276. catch (SQLiteException ex)
  277. {
  278. throw ex;
  279. }
  280. catch (Exception ex)
  281. {
  282. throw ex;
  283. }
  284. }
  285. return ret;
  286. }
  287. }
  288. }

SQLiteHelp的更多相关文章

  1. [android] SQLite 数据库的升级 和 降级

    public class SqliteHelp extends SQLiteOpenHelper { /* * context:创建数据库所需的 上下文对象 * name: 数据库名字 * facto ...

  2. 要知道的DbProviderFactory

    了解DbProviderFactory 前不久想使用下EF的通用单表增删改的特性,当时使用控制台做测试,怎么弄都没成功,原因出在app.config中,反而在mvc项目中,就没有任何问题.在反复的更改 ...

  3. Unity3D 使用SQLite

    使用Unity操作SQLite,需要用到三个库文件,分别是Mono.Data.Sqlite.dll和System.Data.dll和Sqlite3.dll,前两个库文件可以在unity中找到,具体步骤 ...

随机推荐

  1. 6) 十分钟学会android--Activity的生命周期之启动与销毁

    当用户导航.退出和返回您的应用时,应用中的 Activity 实例将在其生命周期中转换不同状态. 例如,当您的Activity初次开始时,它将出现在系统前台并接收用户焦点. 在这个过程中,Androi ...

  2. vue-cli 2.0 常用命令

    一.查询npm版本 npm -v 二.安装npm npm install npm g 三.安装webpack npm install webpack -g 四.安装vue命令行工具 npm insta ...

  3. H5动效的常见制作手法

    众所周知,一个元素,动往往比静更吸引眼球: 一套操作界面,合适的动态交互反馈能给用户带来更好的操作体验: 一个H5运营宣传页,炫酷的动画特效定能助力传播和品牌打造. 近两年,小到loading动画,表 ...

  4. vc++如何创建程序-设置断点-函数的覆盖,c++的多态性

    ---恢复内容开始--- 如何设置断点小笔记 将光标移动到你想设置断点的地方,按一下F9键即可,或者你可以用鼠标左键点击小手图标. CommentOut多行注释 函数的覆盖是在父类与子类之间的,函数的 ...

  5. day27-2 pandas模块

    目录 pandas Series(了解) DataFrame 内置方法 处理缺失值 合并数据 取值 把表格传入excel文件中 把表格从excel中取出来 高级(了解) pandas 处理表格等文件/ ...

  6. python之openpyxl模块

    一 . Python操作EXCEL库的简介 1.1 Python官方库操作excel Python官方库一般使用xlrd库来读取Excel文件,使用xlwt库来生成Excel文件,使用xlutils库 ...

  7. iview datepicker 选择的时间少一天

    使用iview的datepicker时间选择器发现获取的value值是比实际要少一天,严格来说应该是时间格式不一样,datepicker获取的时间是UTC时间格式,也就是:yyyy-MM-ddTHH: ...

  8. react实现简单倒计时

    今天遇到一个简单的小功能,看网上的一些方法感觉不太适合,所以就手敲了一个,直接上代码!!! import React, { Component } from 'react'; class NoTime ...

  9. POJ 1061 青蛙的约会( 拓欧经典题 )

    链接:传送门 思路:简单拓展欧几里德,分析后可以得到方程 x + m * t = y + n * t + L * s( s控制圈数,t代表跳t次会碰面 ),经过化简可以得到 ( n - m ) * t ...

  10. C语言求大数的阶乘

    我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,该如何计算? 当一个数很大时,利用平常的方法是求不出来它的阶乘的,因为数据超出了范围.因此我们要用数组来求一个大数的阶乘,用数组的每位表示结果的 ...