SQLiteHelper.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System.Data;
  2. using System.Data.SQLite;
  3. using System.Windows.Forms;
  4. namespace PaintDotNet.Base.CommTool
  5. {
  6. public class SQLiteHelper
  7. {
  8. SQLiteConnection connection = null;
  9. SQLiteTransaction transaction = null;
  10. string conn_str = "";
  11. //----创建连接串并连接数据库----
  12. public SQLiteHelper()
  13. {
  14. string path = Application.StartupPath + "\\microscope.db3";
  15. string password = "";
  16. conn_str = "data source=" + path + ";password=" + password;
  17. }
  18. //public SQLiteHelper(string path, string password)
  19. //{
  20. // conn_str = "data source=" + path + ";password=" + password;
  21. //}
  22. public bool Connect()
  23. {
  24. try
  25. {
  26. if (connection != null)
  27. {
  28. connection.Close();
  29. connection = null;
  30. }
  31. connection = new SQLiteConnection(conn_str);
  32. connection.Open();
  33. if (connection == null)
  34. {
  35. return false;
  36. }
  37. return true;
  38. }
  39. catch (SQLiteException ex)
  40. {
  41. System.Console.WriteLine(ex.ToString());
  42. return false;
  43. }
  44. }
  45. //----修改数据库密码----
  46. public bool ChangePassword(string newPassword)
  47. {
  48. try
  49. {
  50. connection.ChangePassword(newPassword);
  51. return true;
  52. }
  53. catch (SQLiteException ex)
  54. {
  55. System.Console.WriteLine(ex.ToString());
  56. return false;
  57. }
  58. }
  59. //----关闭数据库连接----
  60. public bool DisConnect()
  61. {
  62. try
  63. {
  64. if (connection != null)
  65. {
  66. connection.Close();
  67. connection = null;
  68. }
  69. return true;
  70. }
  71. catch (SQLiteException ex)
  72. {
  73. System.Console.WriteLine(ex.ToString());
  74. return false;
  75. }
  76. }
  77. /// <summary>
  78. /// 执行一个查询语句,返回一个包含查询结果的DataTable
  79. /// </summary>
  80. /// <param name="sql">要执行的查询语句</param>
  81. /// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>
  82. /// <returns></returns>
  83. public DataTable ExecuteDataTable(string sql, SQLiteParameter[] parameters)
  84. {
  85. try
  86. {
  87. using (SQLiteCommand Command = new SQLiteCommand(sql, connection))
  88. {
  89. if (parameters != null)
  90. {
  91. Command.Parameters.AddRange(parameters);
  92. }
  93. SQLiteDataAdapter adapter = new SQLiteDataAdapter(Command);
  94. DataTable dataTable = new DataTable();
  95. adapter.Fill(dataTable);
  96. return dataTable;
  97. }
  98. }
  99. catch (SQLiteException ex)
  100. {
  101. System.Console.WriteLine(ex.ToString());
  102. return null;
  103. }
  104. }
  105. /// <summary>
  106. /// 对SQLite数据库执行增删改操作,返回受影响的行数。
  107. /// </summary>
  108. /// <param name="sql">要执行的增删改的SQL语句</param>
  109. /// <param name="parameters">执行增删改语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>
  110. /// <returns></returns>
  111. public int ExecuteNonQuery(string sql, SQLiteParameter[] parameters)
  112. {
  113. int affectRows = 0;
  114. try
  115. {
  116. using (SQLiteTransaction Transaction = connection.BeginTransaction())
  117. {
  118. using (SQLiteCommand Command = new SQLiteCommand(sql, connection, Transaction))
  119. {
  120. if (parameters != null)
  121. {
  122. Command.Parameters.AddRange(parameters);
  123. }
  124. affectRows = Command.ExecuteNonQuery();
  125. }
  126. Transaction.Commit();
  127. }
  128. }
  129. catch (SQLiteException ex)
  130. {
  131. System.Console.WriteLine(ex.ToString());
  132. affectRows = -1;
  133. }
  134. return affectRows;
  135. }
  136. //收缩数据库 VACUUM
  137. public bool Vacuum()
  138. {
  139. try
  140. {
  141. using (SQLiteCommand Command = new SQLiteCommand("VACUUM", connection))
  142. {
  143. Command.ExecuteNonQuery();
  144. }
  145. return true;
  146. }
  147. catch (SQLiteException ex)
  148. {
  149. System.Console.WriteLine(ex.ToString());
  150. return false;
  151. }
  152. }
  153. public void BeginTransaction()
  154. {
  155. try
  156. {
  157. transaction = connection.BeginTransaction();
  158. }
  159. catch (SQLiteException ex)
  160. {
  161. System.Console.WriteLine(ex.ToString());
  162. }
  163. }
  164. public void CommitTransaction()
  165. {
  166. try
  167. {
  168. transaction.Commit();
  169. }
  170. catch (SQLiteException ex)
  171. {
  172. System.Console.WriteLine(ex.ToString());
  173. }
  174. }
  175. public void RollbackTransaction()
  176. {
  177. try
  178. {
  179. transaction.Rollback();
  180. }
  181. catch (SQLiteException ex)
  182. {
  183. System.Console.WriteLine(ex.ToString());
  184. }
  185. }
  186. public void test()
  187. {
  188. //SQLiteHelper helper = new SQLiteHelper("D:\\mysqlite.db", "123456"); //连接到D盘下的mysqlite.db数据库,连接密码为123456
  189. SQLiteHelper helper = new SQLiteHelper();
  190. //bool ch = helper.ChangePassword("654321"); //将密码修改为:654321
  191. helper.Connect();
  192. string select_sql = "select * from student"; //查询的SQL语句
  193. DataTable dt = helper.ExecuteDataTable(select_sql, null); //执行查询操作,结果存放在dt中
  194. //向数据库中student表中插入了一条(name = "马兆瑞",sex = "男",telephone = "15550000000")的记录
  195. string insert_sql = "insert into student(name, sex, telephone) values(?,?,?)"; //插入的SQL语句(带参数)
  196. SQLiteParameter[] para = new SQLiteParameter[3]; //构造并绑定参数
  197. string[] tag = { "name", "sex", "telephone" };
  198. string[] value = { "马兆瑞", "男", "15550000000" };
  199. for (int i = 0; i < 3; i++)
  200. {
  201. para[i] = new SQLiteParameter(tag[i], value[i]);
  202. }
  203. int ret = helper.ExecuteNonQuery(insert_sql, para); //执行插入操作
  204. }
  205. }
  206. }