SQLiteHelper2.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SQLite;
  4. using System.Data;
  5. namespace OTSModelSharp.DTLBase
  6. {
  7. namespace Helper
  8. {
  9. public class SQLiteHelper2
  10. {
  11. private static string connectionString = string.Empty;
  12. /// <summary>
  13. /// 根据数据源、密码、版本号设置连接字符串。
  14. /// </summary>
  15. /// <param name="datasource">数据源。</param>
  16. /// <param name="password">密码。</param>
  17. /// <param name="version">版本号(缺省为3)。</param>
  18. public static void SetConnectionString(string datasource, string password, int version = 3)
  19. {
  20. connectionString = string.Format("Data Source={0};Version={1};password={2}",
  21. datasource, version, password);
  22. }
  23. /// <summary>
  24. /// 创建一个数据库文件。如果存在同名数据库文件,则会覆盖。
  25. /// </summary>
  26. /// <param name="dbName">数据库文件名。为null或空串时不创建。</param>
  27. /// <param name="password">(可选)数据库密码,默认为空。</param>
  28. /// <exception cref="Exception"></exception>
  29. public static void CreateDB(string dbName)
  30. {
  31. if (!string.IsNullOrEmpty(dbName))
  32. {
  33. try { SQLiteConnection.CreateFile(dbName); }
  34. catch (Exception) { throw; }
  35. }
  36. }
  37. /// <summary>
  38. /// 对SQLite数据库执行增删改操作,返回受影响的行数。
  39. /// </summary>
  40. /// <param name="sql">要执行的增删改的SQL语句。</param>
  41. /// <param name="parameters">执行增删改语句所需要的参数,参数必须以它们在SQL语句中的顺序为准。</param>
  42. /// <returns></returns>
  43. /// <exception cref="Exception"></exception>
  44. public int ExecuteNonQuery(string sql, params SQLiteParameter[] parameters)
  45. {
  46. int affectedRows = 0;
  47. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  48. {
  49. using (SQLiteCommand command = new SQLiteCommand(connection))
  50. {
  51. try
  52. {
  53. connection.Open();
  54. command.CommandText = sql;
  55. if (parameters.Length != 0)
  56. {
  57. command.Parameters.AddRange(parameters);
  58. }
  59. affectedRows = command.ExecuteNonQuery();
  60. }
  61. catch (Exception) { throw; }
  62. }
  63. }
  64. return affectedRows;
  65. }
  66. /// <summary>
  67. /// 批量处理数据操作语句。
  68. /// </summary>
  69. /// <param name="list">SQL语句集合。</param>
  70. /// <exception cref="Exception"></exception>
  71. public void ExecuteNonQueryBatch(List<KeyValuePair<string, SQLiteParameter[]>> list)
  72. {
  73. using (SQLiteConnection conn = new SQLiteConnection(connectionString))
  74. {
  75. try { conn.Open(); }
  76. catch { throw; }
  77. using (SQLiteTransaction tran = conn.BeginTransaction())
  78. {
  79. using (SQLiteCommand cmd = new SQLiteCommand(conn))
  80. {
  81. try
  82. {
  83. foreach (var item in list)
  84. {
  85. cmd.CommandText = item.Key;
  86. if (item.Value != null)
  87. {
  88. cmd.Parameters.AddRange(item.Value);
  89. }
  90. cmd.ExecuteNonQuery();
  91. }
  92. tran.Commit();
  93. }
  94. catch (Exception) { tran.Rollback(); throw; }
  95. }
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// 执行查询语句,并返回第一个结果。
  101. /// </summary>
  102. /// <param name="sql">查询语句。</param>
  103. /// <returns>查询结果。</returns>
  104. /// <exception cref="Exception"></exception>
  105. public object ExecuteScalar(string sql, params SQLiteParameter[] parameters)
  106. {
  107. using (SQLiteConnection conn = new SQLiteConnection(connectionString))
  108. {
  109. using (SQLiteCommand cmd = new SQLiteCommand(conn))
  110. {
  111. try
  112. {
  113. conn.Open();
  114. cmd.CommandText = sql;
  115. if (parameters.Length != 0)
  116. {
  117. cmd.Parameters.AddRange(parameters);
  118. }
  119. return cmd.ExecuteScalar();
  120. }
  121. catch (Exception) { throw; }
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// 执行一个查询语句,返回一个包含查询结果的DataTable。
  127. /// </summary>
  128. /// <param name="sql">要执行的查询语句。</param>
  129. /// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准。</param>
  130. /// <returns></returns>
  131. /// <exception cref="Exception"></exception>
  132. public DataTable ExecuteQuery(string sql, params SQLiteParameter[] parameters)
  133. {
  134. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  135. {
  136. using (SQLiteCommand command = new SQLiteCommand(sql, connection))
  137. {
  138. if (parameters.Length != 0)
  139. {
  140. command.Parameters.AddRange(parameters);
  141. }
  142. SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
  143. DataTable data = new DataTable();
  144. try { adapter.Fill(data); }
  145. catch (Exception) { throw; }
  146. return data;
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// 执行一个查询语句,返回一个关联的SQLiteDataReader实例。
  152. /// </summary>
  153. /// <param name="sql">要执行的查询语句。</param>
  154. /// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准。</param>
  155. /// <returns></returns>
  156. /// <exception cref="Exception"></exception>
  157. public SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] parameters)
  158. {
  159. SQLiteConnection connection = new SQLiteConnection(connectionString);
  160. SQLiteCommand command = new SQLiteCommand(sql, connection);
  161. try
  162. {
  163. if (parameters.Length != 0)
  164. {
  165. command.Parameters.AddRange(parameters);
  166. }
  167. connection.Open();
  168. return command.ExecuteReader(CommandBehavior.CloseConnection);
  169. }
  170. catch (Exception) { throw; }
  171. }
  172. /// <summary>
  173. /// 查询数据库中的所有数据类型信息。
  174. /// </summary>
  175. /// <returns></returns>
  176. /// <exception cref="Exception"></exception>
  177. public DataTable GetSchema()
  178. {
  179. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  180. {
  181. try
  182. {
  183. connection.Open();
  184. return connection.GetSchema("TABLES");
  185. }
  186. catch (Exception) { throw; }
  187. }
  188. }
  189. }
  190. }
  191. }