zoukankan      html  css  js  c++  java
  • SQLiteHelp

    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. }





  • 相关阅读:
    浅谈iOS的SDK与API
    ulua-应用层模块编码
    ErlangRoad_2
    ErlangRoad_1
    Git笔记--git使用基本概念和术语
    小米Pro 15.6 系统重装记录
    Ubuntu14.04安装opencv2.4.13
    ubuntu配置机器学习环境(四) 安装intel MKL
    ubuntu配置机器学习环境(三) opencv 安装
    ubuntu配置机器学习环境(二) cuda 和cudnn 安装
  • 原文地址:https://www.cnblogs.com/BookCode/p/5949450.html
Copyright © 2011-2022 走看看