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. 在AndroidManifest(清单文件)中注册activity(活动)及配置主活动、更改App图标、App名称、修改隐藏标题栏

    打开app/src/main/AndroidManifest. <?xml version="1.0" encoding="utf-8"?> < ...

  2. Windows 下修改 MySQL 编码为 utf8

    问题 Windows 下安装 MySQL 后,默认编码不全utf8. mysql> show variables like '%char%'; +------------------------ ...

  3. readonly and const variable

    共同点:都是常量: 不同点:const的值必须在编译前确定,通常在声明的同时赋值:而readonly可在运行时确定:

  4. size_t与size_type的使用

    size_t 是为了方便系统之间的移植而定义的 在32位系统上 定义为 unsigned int在64位系统上 定义为 unsigned long 更准确地说法是 在 32位系统上是32位无符号整形在 ...

  5. ReactiveX Operators

    This documentation groups information about the various operators and examples of their usage into t ...

  6. Linux搭建Jenkins

    1.添加存储库 yum的repo中默认没有Jenkins,需要先将Jenkins存储库添加到yum repos,执行下面的命令: 使用wget -O下载并以不同的文件名保存 [root@besttes ...

  7. jmeter实现逻辑控制器

  8. crontab 设置定时任务

    查看当前用户已有的定时任务: crontab -l 编辑crontab: crontab -e 加入需要执行的命令: 0 */4 * * * /www/shwww.net/venv/bin/pytho ...

  9. easyui获取当前点击对象tabs的title和Index

    观察上面打开的tabs选项卡,肯定会有一个目前是被选中状态,而这个状态的class属性也肯定是和其他tabs不一样的,有个class等于tabs-selected的 var title = $('.t ...

  10. nyoj 803 大数问题

    #include<stdio.h> #include<string.h> #define ll long long #define N 110000 int main() { ...