C# SQLite 简单封装

发布时间:2016-12-10 19:01:34 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"C# SQLite 简单封装",主要涉及到C# SQLite 简单封装方面的内容,对于C# SQLite 简单封装感兴趣的同学可以参考一下。

SQLiteHelper类 1 using System; 2 using System.Configuration; 3 using System.Data; 4 using System.Data.SQLite; 5 6 namespace MyBlog.Data 7 { 8 9 /// <summary>10 /// SQLiteHelper11 /// </summary>12 public class SQLiteHelper : System.IDisposable13 {14 private SQLiteConnection _SQLiteConn = null;15 private SQLiteTransaction _SQLiteTrans = null;16 private bool _IsRunTrans = false;17 private string _SQLiteConnString = null;18 private bool _disposed = false;19 private bool _autocommit = false;20 #region 构造/析构函数21 /// <summary>22 /// 初始化 SQLiteHelper23 24 /// </summary>25 public SQLiteHelper()26 : this(ConfigurationManager.ConnectionStrings["SQLite"].ConnectionString)27 {28 }29   1 /// <summary> 2 /// 初始化 SQLiteHelper 3 /// </summary> 4 /// <param name="connectionstring">数据库连接字符串</param> 5 public SQLiteHelper(string connectionstring) 6 { 7 this._SQLiteConnString = connectionstring; 8 this._SQLiteConn = new SQLiteConnection(this._SQLiteConnString); 9 this._SQLiteConn.Commit += new SQLiteCommitHandler(_SQLiteConn_Commit); 10 this._SQLiteConn.RollBack += new EventHandler(_SQLiteConn_RollBack); 11 } 12 13 /// <summary> 14 /// SQLiteHelper 析构函数 15 /// </summary> 16 ~SQLiteHelper() 17 { 18 this.Dispose(false); 19 } 20 21 #endregion 22 #region 方法 23 /// <summary> 24 /// 打开数据库连接 25 /// </summary> 26 private void Open() 27 { 28 if (this._SQLiteConn.State == ConnectionState.Closed) 29 { 30 this._SQLiteConn.Open(); 31 } 32 } 33 /// <summary> 34 /// 关闭数据库连接 35 /// </summary> 36 private void Close() 37 { 38 if (this._SQLiteConn.State != ConnectionState.Closed) 39 { 40 if (this._IsRunTrans && this._autocommit) 41 { 42 this.Commit(); 43 } 44 this._SQLiteConn.Close(); 45 } 46 } 47 /// <summary> 48 /// 开始数据库事务 49 /// </summary> 50 public void BeginTransaction() 51 { 52 this._SQLiteConn.BeginTransaction(); 53 this._IsRunTrans = true; 54 } 55 /// <summary> 56 /// 开始数据库事务 57 /// </summary> 58 /// <param name="isoLevel">事务锁级别</param> 59 public void BeginTransaction(IsolationLevel isoLevel) 60 { 61 this._SQLiteConn.BeginTransaction(isoLevel); 62 this._IsRunTrans = true; 63 } 64 /// <summary> 65 /// 提交当前挂起的事务 66 /// </summary> 67 public void Commit() 68 { 69 if (this._IsRunTrans) 70 { 71 this._SQLiteTrans.Commit(); 72 this._IsRunTrans = false; 73 } 74 } 75 /// <summary> 76 /// 回滚当前挂起的事务 77 /// </summary> 78 public void Rollback() 79 { 80 if (this._IsRunTrans) 81 { 82 this._SQLiteTrans.Rollback(); 83 this._IsRunTrans = false; 84 } 85 } 86 /// <summary> 87 /// 执行SQL语句 88 /// </summary> 89 /// <param name="command">SQL语句</param> 90 /// <returns>返回受影响行数 [SELECT 不会返回影响行]</returns> 91 public int Execute(string command) 92 { 93 int result = -1; 94 this.Open(); 95 using (SQLiteCommand sqlitecmd = new SQLiteCommand(command)) 96 { 97 result = sqlitecmd.ExecuteNonQuery(); 98 } 99 this.Close();100 return result;101 }102 /// <summary>103 /// 执行SQL语句104 /// </summary>105 /// <param name="command">SQL语句</param>106 /// <param name="parameter">参数组</param>107 /// <returns>返回受影响行数 [SELECT 不会返回影响行]</returns>108 public int Execute(string command, SQLiteParameter[] parameter)109 {110 int result = -1;111 this.Open();112 using (SQLiteCommand sqlitecmd = new SQLiteCommand(command))113 {114 sqlitecmd.Parameters.AddRange(parameter);115 result = sqlitecmd.ExecuteNonQuery();116 }117 this.Close();118 return result;119 }120 /// <summary>121 /// 执行SQL语句122 /// </summary>123 /// <param name="command">SQL语句</param>124 /// <returns>返回第一行第一列值</returns>125 public object ExecuteScalar(string command)126 {127 object result = null;128 this.Open();129 using (SQLiteCommand sqlitecmd = new SQLiteCommand(command))130 {131 result = sqlitecmd.ExecuteScalar();132 }133 this.Close();134 return result;135 }136 /// <summary>137 /// 执行SQL语句138 /// </summary>139 /// <param name="command">SQL语句</param>140 /// <param name="parmeter">参数组</param>141 /// <returns>返回受影响行数 [SELECT 不会返回影响行]</returns>142 public object ExecuteScalar(string command, SQLiteParameter[] parmeter)143 {144 object result = null;145 this.Open();146 using (SQLiteCommand sqlitecmd = new SQLiteCommand(command))147 {148 sqlitecmd.Parameters.AddRange(parmeter);149 result = sqlitecmd.ExecuteScalar();150 }151 this.Close();152 return result;153 }154 /// <summary>155 /// 执行SQL语句156 /// </summary>157 /// <param name="command">SQL语句</param>158 /// <returns>返回DataSet数据集</returns>159 public DataSet GetDs(string command)160 {161 return this.GetDs(command, string.Empty);162 }163 public DataSet GetDs(string command, string tablename)164 {165 DataSet ds = new DataSet();166 this.Open();167 using (SQLiteCommand sqlitecmd = new SQLiteCommand(command, this._SQLiteConn))168 {169 using (SQLiteDataAdapter sqliteadapter = new SQLiteDataAdapter(sqlitecmd))170 {171 if (string.Empty.Equals(tablename))172 {173 sqliteadapter.Fill(ds);174 }175 else176 {177 sqliteadapter.Fill(ds, tablename);178 }179 }180 }181 this.Close();182 return ds;183 }184 185 public DataSet GetDs(string command, out SQLiteCommand SqlItecmd)186 {187 return this.GetDs(command, string.Empty, out SqlItecmd);188 }189 190 public DataSet GetDs(string command, string tablename, out SQLiteCommand SqlItecmd)191 {192 DataSet ds = new DataSet();193 this.Open();194 SQLiteCommand sqlcmd = new SQLiteCommand(command, this._SQLiteConn);195 using (SQLiteDataAdapter sqladapter = new SQLiteDataAdapter(sqlcmd))196 {197 sqladapter.Fill(ds);198 }199 SqlItecmd = sqlcmd;200 this.Close();201 return ds;202 }203 204 public int Update(DataSet ds, ref SQLiteCommand SqlItecmd)205 {206 return this.Update(ds, string.Empty, ref SqlItecmd);207 }208 209 public int Update(DataSet ds, string tablename, ref SQLiteCommand SqlItecmd)210 {211 int result = -1;212 this.Open();213 using (SQLiteDataAdapter sqladapter = new SQLiteDataAdapter(SqlItecmd))214 {215 using (SQLiteCommandBuilder sqlcommandbuilder = new SQLiteCommandBuilder(sqladapter))216 {217 if (string.Empty.Equals(tablename))218 {219 result = sqladapter.Update(ds);220 }221 else222 {223 result = sqladapter.Update(ds, tablename);224 }225 }226 }227 this.Close();228 return result;229 }230 /// <summary>231 /// 释放该实例的托管资源232 /// </summary>233 public virtual void Dispose()234 {235 this.Dispose(true);236 GC.SuppressFinalize(this);237 }238 protected void Dispose(bool disposing)239 {240 if (!this._disposed)241 {242 if (disposing)243 {244 // 定义释放非托管资源245 }246 this._disposed = true;247 }248 }249 #endregion250 #region 属性251 /// <summary>252 /// 获取数据库连接字符串253 /// </summary>254 public string ConnectionString255 {256 get257 {258 return this._SQLiteConnString;259 }260 }261 /// <summary>262 /// 设置是否自动提交事务263 /// </summary>264 public bool AutoCommit265 {266 get267 {268 return this._autocommit;269 }270 set271 {272 this._autocommit = value;273 }274 }275 #endregion276 #region 事件277 void _SQLiteConn_RollBack(object sender, EventArgs e)278 {279 this._IsRunTrans = false;280 }281 void _SQLiteConn_Commit(object sender, CommitEventArgs e)282 {283 this._IsRunTrans = false;284 }285 #endregion286 }287 }  

上一篇:给Visual Studio 2010添加Windows Phone 7模板
下一篇:android 电子书源码,翻页效果

相关文章

相关评论