Sqlite DBconnection DAL类

前端之家收集整理的这篇文章主要介绍了Sqlite DBconnection DAL类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. public class sqliteDataModel
  2. {
  3. private sqliteCommand sqliteCommand;
  4. private sqliteDataAdapter sqliteAdpater;
  5. private sqliteTransaction sqliteTransaction;
  6.  
  7. private sqliteConnection strConn = null;
  8. public sqliteConnection StrConn
  9. {
  10. get
  11. {
  12. if (strConn == null)
  13. {
  14. string strConnstring = AppDomain.CurrentDomain.BaseDirectory+"test.DB3";
  15. if (!File.Exists(strConnstring))
  16. {
  17. sqliteConnection.CreateFile(strConnstring);
  18. sqliteConnectionStringBuilder connStr = new sqliteConnectionStringBuilder();
  19. connStr.DataSource = strConnstring;
  20. strConn = new sqliteConnection(connStr.ToString());
  21. if (strConn.State == ConnectionState.Closed)
  22. {
  23. //修改这个参数的默认值的原因是删除数据之后会自动清理数据文件
  24. strConn.Open();
  25. sqliteCommand cmd = new sqliteCommand();
  26. cmd.CommandText = " PRAGMA auto_vacuum = 1";
  27. cmd.Connection = strConn;
  28. cmd.ExecuteNonQuery();
  29. strConn.Close();
  30. }
  31. }
  32. else
  33. {
  34. sqliteConnectionStringBuilder connStr = new sqliteConnectionStringBuilder();
  35. connStr.DataSource = strConnstring;
  36. strConn = new sqliteConnection(connStr.ToString());
  37. }
  38. }
  39. return strConn;
  40. }
  41. }
  42. public sqliteDataModel()
  43. {
  44. this.Init();
  45. }
  46. private void Init()
  47. {
  48. if (this.sqliteCommand != null)
  49. {
  50. this.sqliteCommand.Dispose();
  51. this.sqliteCommand = null;
  52. }
  53. if (this.sqliteAdpater != null)
  54. {
  55. this.sqliteAdpater.Dispose();
  56. this.sqliteAdpater = null;
  57. }
  58. if (this.sqliteTransaction != null)
  59. {
  60. this.sqliteTransaction.Dispose();
  61. this.sqliteTransaction = null;
  62. }
  63. if (this.strConn != null)
  64. {
  65. this.strConn.Dispose();
  66. }
  67. this.sqliteCommand = new sqliteCommand();
  68. this.sqliteAdpater = new sqliteDataAdapter();
  69. this.sqliteCommand.Connection = this.StrConn;
  70. this.sqliteAdpater.SelectCommand = this.sqliteCommand;
  71. }
  72.  
  73. public int ExeNonQuery(string strsql)
  74. {
  75. int nReturn = 0;
  76. this.sqliteCommand.CommandText = strsql;
  77. try
  78. {
  79. if (this.StrConn.State == ConnectionState.Closed)
  80. {
  81. this.StrConn.Open();
  82. nReturn = sqliteCommand.ExecuteNonQuery();
  83. this.StrConn.Close();
  84. }
  85. else
  86. {
  87. nReturn = sqliteCommand.ExecuteNonQuery();
  88. }
  89. }
  90. catch (sqliteException ex)
  91. {
  92. Pub.WriteLog(ex.StackTrace);
  93. this.StrConn.Close();
  94. return nReturn;
  95. }
  96. return nReturn;
  97. }
  98. public DataTable ExeQuery(string strsql)
  99. {
  100. this.sqliteCommand.CommandText = strsql;
  101. DataTable dtResult = new DataTable();
  102. try
  103. {
  104. if (this.StrConn.State == ConnectionState.Closed)
  105. {
  106. this.StrConn.Open();
  107. this.sqliteAdpater.Fill(dtResult);
  108. this.StrConn.Close();
  109. }
  110. else
  111. {
  112. this.sqliteAdpater.Fill(dtResult);
  113. }
  114. }
  115. catch (sqliteException ex)
  116. {
  117. Pub.WriteLog(ex.StackTrace);
  118. return null;
  119. }
  120. return dtResult;
  121. }
  122. /// <summary>
  123. /// 存储二进制文件
  124. /// </summary>
  125. /// <param name="buf">文件缓存</param>
  126. /// <param name="sql"></param>
  127. /// <param name="para">占位符</param>
  128. /// <returns></returns>
  129. public bool ExeNonQuery(byte[] buf,string sql,string para)
  130. {
  131. lock (this)
  132. {
  133. this.sqliteCommand.CommandText = sql;
  134. try
  135. {
  136. if (this.StrConn.State == ConnectionState.Closed)
  137. {
  138. this.StrConn.Open();
  139. this.sqliteTransaction = this.StrConn.BeginTransaction();
  140. this.sqliteCommand.Transaction = this.sqliteTransaction;
  141. sqliteCommand.Parameters.Add(para,DbType.Binary).Value = buf;
  142. sqliteCommand.ExecuteNonQuery();
  143. this.sqliteTransaction.Commit();
  144. this.StrConn.Close();
  145. }
  146. else
  147. {
  148. this.sqliteTransaction = this.StrConn.BeginTransaction();
  149. this.sqliteCommand.Transaction = this.sqliteTransaction;
  150. sqliteCommand.Parameters.Add(para,DbType.Binary).Value = buf;
  151. sqliteCommand.ExecuteNonQuery();
  152. this.sqliteTransaction.Commit();
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. Pub.WriteLog(ex.StackTrace);
  158. this.sqliteTransaction.Rollback();
  159. this.StrConn.Close();
  160. return false;
  161. }
  162. }
  163. return true;
  164. }
  165.  
  166. public sqliteDataReader ExeQueryDataReader(string strsql)
  167. {
  168. this.sqliteCommand.CommandText = strsql;
  169. sqliteDataReader dr;
  170. lock (this)
  171. {
  172. try
  173. {
  174. if (this.StrConn.State == ConnectionState.Closed)
  175. {
  176. this.StrConn.Open();
  177. dr = this.sqliteCommand.ExecuteReader();
  178. this.StrConn.Close();
  179. }
  180. else
  181. {
  182. dr = this.sqliteCommand.ExecuteReader();
  183. }
  184. }
  185. catch (System.Data.sqlite.sqliteException Ex)
  186. {
  187. this.StrConn.Close();
  188. Pub.WriteLog(Ex.StackTrace);
  189. return null;
  190. }
  191. }
  192. return dr;
  193. }
  194.  
  195. public void BeginTransaction()
  196. {
  197. lock (this)
  198. {
  199. if (this.StrConn.State != ConnectionState.Open)
  200. {
  201. this.StrConn.Open();
  202. }
  203. this.sqliteTransaction = this.StrConn.BeginTransaction();
  204. }
  205. }
  206. public void CommitTransaction()
  207. {
  208. lock (this)
  209. {
  210. this.sqliteTransaction.Commit();
  211. this.sqliteTransaction.Dispose();
  212. this.sqliteTransaction = null;
  213. if (this.StrConn.State != ConnectionState.Closed)
  214. {
  215. this.StrConn.Close();
  216. }
  217. }
  218. }
  219. public void RollBackTransaction()
  220. {
  221. lock (this)
  222. {
  223. try
  224. {
  225. this.sqliteTransaction.Rollback();
  226. }
  227. catch (Exception ex)
  228. {
  229. Pub.WriteLog(ex.StackTrace);
  230. }
  231. this.sqliteTransaction.Dispose();
  232. this.sqliteTransaction = null;
  233. if (this.StrConn.State != ConnectionState.Closed)
  234. {
  235. this.StrConn.Close();
  236. }
  237. }
  238. }
  239. }

猜你在找的Sqlite相关文章