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. Android 通用Dialog中设置RecyclerView

    通用Dialog public class IOSRecyclerViewDialog{ private Context context; private Dialog dialog; private ...

  2. Wireshark抓包过滤

    主要说明下抓包前准备工作,及wireshark里面的两个过滤器:捕获过滤器和应用显示过滤器 1.捕获过滤器.顾名思义就是捕获时的过滤器.主要用来决定你要抓包抓哪个IP哪个端口,明确自己要抓哪个IP和端 ...

  3. facebook atc弱网环境搭建和踩坑总结

    facebook atc介绍 Augmented Traffic Control(又名atc)是一种模拟网络状况的工具.由facebook开源,是一个允许开发人员控制设备与互联网连接的项目.atc可以 ...

  4. js 正则 测试

    var str0 = '2013-6-7()'; var AAAAADD = str0.match(/(\d)/g); //11个空字符串 //["2", "0" ...

  5. 谈谈网页中的ajax

    一个页面上有很多的ajax请求,这样的页面右键查看源文件是没法看到全部的html.事实上,这种网页也是从正常的html页面改造过来的,常用的一个场景是,同一个区域大量循环,在动态页面里(比如jsp等) ...

  6. 日常记录-Pandas Cookbook

    Cookbook 1.更新内容 2.关于安装 3.Pandas使用注意事项 4.包环境 5.10分钟Pandas初识 6.教程 7.Cookbook 8.数据结构简介 9.基本功能 10.使用文本数据 ...

  7. background及background-size

    background有以下几种属性: background-color background-position background-size background-repeat background ...

  8. Vue学习之路第十三篇:v-for指令

    v-for指令,看名字想必大家也能猜到其作用,没错,就是用来迭代.遍历的. 1.简单数组的遍历 <body> <divi id="app"> <spa ...

  9. webpack学习笔记(3)--webpack.config.js

    module 参数 使用下面的实例来说明 module.exports = { module: { rules: [ { test: /\.css$/, use: 'css-loader' }, { ...

  10. rabbitMQ学习笔记(五) 消息路由

    生产者会生产出很多消息 , 但是不同的消费者可能会有不同的需求,只需要接收指定的消息,其他的消息需要被过滤掉. 这时候就可以对消息进行过滤了. 在消费者端设置好需要接收的消息类型. 如果不使用默认的E ...