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. Struts2框架学习(二)——访问流程及架构

    1.Struts2的执行流程 从客户端发送请求过来,先经过前端控制器(核心过滤器StrutsPrepareAndExecuteFilter)过滤器中执行一组拦截器(一组拦截器就会完成部分功能代码),拦 ...

  2. CUDA与OpenCL架构

    CUDA与OpenCL架构 目录 CUDA与OpenCL架构 目录 1 GPU的体系结构 1.1 GPU简介 1.2 GPU与CPU的差异 2 CUDA架构 2.1 硬件架构 2.1.1 GPU困境 ...

  3. 【从零开始】【Java】【2】项目最开始都有什么鬼

    闲聊 刨其根知其底. 让我们从一开始就慢嚼细咽. 开始 先来看下项目都有什么: 项目结构图 pom文件图 项目结构 项目=核心代码+依赖管理文件+说明文件+IDE配套文件+外部依赖包: 核心代码:sr ...

  4. Java之关于面向对象

    面向对象,呃,别给我说程序员找不到对象,那是windows才会出现的情况~~~ 就简单记下笔记什么的吧. 1.关于定义和赋值 之前总是搞混淆,说到底是没有搞清楚. shit bigOne=new sh ...

  5. MySQL数据库各个版本的区别

    MySQL数据库各个版本的区别 MySQL数据库 MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),MySQL数据库系统使用最常用的数据库管理语言--结构化查询语言(SQL)进行数据库管 ...

  6. RabbitMQ基础知识(转载)

    RabbitMQ基础知识(转载) 一.背景 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需 ...

  7. [SCOI2008]奖励关_状压动归_数学期望

    Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 20; dou ...

  8. web前端知识框架

  9. Linux磁盘分区--GPT分区

    MBR分区表有一定的局限性,最大支持2.1tb硬盘,单块硬盘最多4个主分区. 这里就要引入GPT分区表,可以支持最大18EB的卷,最多支持128个主分区,所以如果使用大于2tb的卷,就必须使用GTP分 ...

  10. python 中的一些小命令