C#封装的Sqlite访问类实例
本文实例讲述了C#封装的Sqlite访问类。分享给大家供大家参考。具体分析如下:
C#封装的Sqlite访问类,要访问Sqlite这下简单了,直接调用此类中的方法即可
usingSystem; usingSystem.Collections; usingSystem.Collections.Specialized; usingSystem.Data; usingSystem.Configuration; usingSystem.Data.SQLite; namespaceDAL { internalabstractclassDbHelperSQLite { publicstaticstringconnectionString="DataSource="+AppDomain.CurrentDomain.BaseDirectory+@"dataleaf.db;Version=3;"; publicDbHelperSQLite() { } #region公用方法 publicstaticintGetMaxID(stringFieldName,stringTableName) { stringstrsql="selectmax("+FieldName+")+1from"+TableName; objectobj=GetSingle(strsql); if(obj==null) { return1; } else { returnint.Parse(obj.ToString()); } } publicstaticboolExists(stringstrSql) { objectobj=GetSingle(strSql); intcmdresult; if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value))) { cmdresult=0; } else { cmdresult=int.Parse(obj.ToString()); } if(cmdresult==0) { returnfalse; } else { returntrue; } } publicstaticboolExists(stringstrSql,paramsSQLiteParameter[]cmdParms) { objectobj=GetSingle(strSql,cmdParms); intcmdresult; if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value))) { cmdresult=0; } else { cmdresult=int.Parse(obj.ToString()); } if(cmdresult==0) { returnfalse; } else { returntrue; } } #endregion #region执行简单SQL语句 ///<summary> ///执行SQL语句,返回影响的记录数 ///</summary> ///<paramname="SQLString">SQL语句</param> ///<returns>影响的记录数</returns> publicstaticintExecuteSql(stringSQLString) { using(SQLiteConnectionconnection=newSQLiteConnection(connectionString)) { using(SQLiteCommandcmd=newSQLiteCommand(SQLString,connection)) { try { connection.Open(); introws=cmd.ExecuteNonQuery(); returnrows; } catch(System.Data.SQLite.SQLiteExceptionE) { connection.Close(); thrownewException(E.Message); } } } } ///<summary> ///执行多条SQL语句,实现数据库事务。 ///</summary> ///<paramname="SQLStringList">多条SQL语句</param> publicstaticvoidExecuteSqlTran(ArrayListSQLStringList) { using(SQLiteConnectionconn=newSQLiteConnection(connectionString)) { conn.Open(); SQLiteCommandcmd=newSQLiteCommand(); cmd.Connection=conn; SQLiteTransactiontx=conn.BeginTransaction(); cmd.Transaction=tx; try { for(intn=0;n<SQLStringList.Count;n++) { stringstrsql=SQLStringList[n].ToString(); if(strsql.Trim().Length>1) { cmd.CommandText=strsql; cmd.ExecuteNonQuery(); } } tx.Commit(); } catch(System.Data.SQLite.SQLiteExceptionE) { tx.Rollback(); thrownewException(E.Message); } } } ///<summary> ///执行带一个存储过程参数的的SQL语句。 ///</summary> ///<paramname="SQLString">SQL语句</param> ///<paramname="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param> ///<returns>影响的记录数</returns> publicstaticintExecuteSql(stringSQLString,stringcontent) { using(SQLiteConnectionconnection=newSQLiteConnection(connectionString)) { SQLiteCommandcmd=newSQLiteCommand(SQLString,connection); SQLiteParametermyParameter=newSQLiteParameter("@content",DbType.String); myParameter.Value=content; cmd.Parameters.Add(myParameter); try { connection.Open(); introws=cmd.ExecuteNonQuery(); returnrows; } catch(System.Data.SQLite.SQLiteExceptionE) { thrownewException(E.Message); } finally { cmd.Dispose(); connection.Close(); } } } ///<summary> ///向数据库里插入图像格式的字段(和上面情况类似的另一种实例) ///</summary> ///<paramname="strSQL">SQL语句</param> ///<paramname="fs">图像字节,数据库的字段类型为image的情况</param> ///<returns>影响的记录数</returns> publicstaticintExecuteSqlInsertImg(stringstrSQL,byte[]fs) { using(SQLiteConnectionconnection=newSQLiteConnection(connectionString)) { SQLiteCommandcmd=newSQLiteCommand(strSQL,connection); SQLiteParametermyParameter=newSQLiteParameter("@fs",DbType.Binary); myParameter.Value=fs; cmd.Parameters.Add(myParameter); try { connection.Open(); introws=cmd.ExecuteNonQuery(); returnrows; } catch(System.Data.SQLite.SQLiteExceptionE) { thrownewException(E.Message); } finally { cmd.Dispose(); connection.Close(); } } } ///<summary> ///执行一条计算查询结果语句,返回查询结果(object)。 ///</summary> ///<paramname="SQLString">计算查询结果语句</param> ///<returns>查询结果(object)</returns> publicstaticobjectGetSingle(stringSQLString) { using(SQLiteConnectionconnection=newSQLiteConnection(connectionString)) { using(SQLiteCommandcmd=newSQLiteCommand(SQLString,connection)) { try { connection.Open(); objectobj=cmd.ExecuteScalar(); if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value))) { returnnull; } else { returnobj; } } catch(System.Data.SQLite.SQLiteExceptione) { connection.Close(); thrownewException(e.Message); } } } } ///<summary> ///执行查询语句,返回SQLiteDataReader ///</summary> ///<paramname="strSQL">查询语句</param> ///<returns>SQLiteDataReader</returns> publicstaticSQLiteDataReaderExecuteReader(stringstrSQL) { SQLiteConnectionconnection=newSQLiteConnection(connectionString); SQLiteCommandcmd=newSQLiteCommand(strSQL,connection); try { connection.Open(); SQLiteDataReadermyReader=cmd.ExecuteReader(); returnmyReader; } catch(System.Data.SQLite.SQLiteExceptione) { thrownewException(e.Message); } } ///<summary> ///执行查询语句,返回DataSet ///</summary> ///<paramname="SQLString">查询语句</param> ///<returns>DataSet</returns> publicstaticDataSetQuery(stringSQLString) { using(SQLiteConnectionconnection=newSQLiteConnection(connectionString)) { DataSetds=newDataSet(); try { connection.Open(); SQLiteDataAdaptercommand=newSQLiteDataAdapter(SQLString,connection); command.Fill(ds,"ds"); } catch(System.Data.SQLite.SQLiteExceptionex) { thrownewException(ex.Message); } returnds; } } #endregion #region执行带参数的SQL语句 ///<summary> ///执行SQL语句,返回影响的记录数 ///</summary> ///<paramname="SQLString">SQL语句</param> ///<returns>影响的记录数</returns> publicstaticintExecuteSql(stringSQLString,paramsSQLiteParameter[]cmdParms) { using(SQLiteConnectionconnection=newSQLiteConnection(connectionString)) { using(SQLiteCommandcmd=newSQLiteCommand()) { try { PrepareCommand(cmd,connection,null,SQLString,cmdParms); introws=cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); returnrows; } catch(System.Data.SQLite.SQLiteExceptionE) { thrownewException(E.Message); } } } } ///<summary> ///执行多条SQL语句,实现数据库事务。 ///</summary> ///<paramname="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SQLiteParameter[])</param> publicstaticvoidExecuteSqlTran(HashtableSQLStringList) { using(SQLiteConnectionconn=newSQLiteConnection(connectionString)) { conn.Open(); using(SQLiteTransactiontrans=conn.BeginTransaction()) { SQLiteCommandcmd=newSQLiteCommand(); try { //循环 foreach(DictionaryEntrymyDEinSQLStringList) { stringcmdText=myDE.Key.ToString(); SQLiteParameter[]cmdParms=(SQLiteParameter[])myDE.Value; PrepareCommand(cmd,conn,trans,cmdText,cmdParms); intval=cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); trans.Commit(); } } catch { trans.Rollback(); throw; } } } } ///<summary> ///执行一条计算查询结果语句,返回查询结果(object)。 ///</summary> ///<paramname="SQLString">计算查询结果语句</param> ///<returns>查询结果(object)</returns> publicstaticobjectGetSingle(stringSQLString,paramsSQLiteParameter[]cmdParms) { using(SQLiteConnectionconnection=newSQLiteConnection(connectionString)) { using(SQLiteCommandcmd=newSQLiteCommand()) { try { PrepareCommand(cmd,connection,null,SQLString,cmdParms); objectobj=cmd.ExecuteScalar(); cmd.Parameters.Clear(); if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value))) { returnnull; } else { returnobj; } } catch(System.Data.SQLite.SQLiteExceptione) { thrownewException(e.Message); } } } } ///<summary> ///执行查询语句,返回SQLiteDataReader ///</summary> ///<paramname="strSQL">查询语句</param> ///<returns>SQLiteDataReader</returns> publicstaticSQLiteDataReaderExecuteReader(stringSQLString,paramsSQLiteParameter[]cmdParms) { SQLiteConnectionconnection=newSQLiteConnection(connectionString); SQLiteCommandcmd=newSQLiteCommand(); try { PrepareCommand(cmd,connection,null,SQLString,cmdParms); SQLiteDataReadermyReader=cmd.ExecuteReader(); cmd.Parameters.Clear(); returnmyReader; } catch(System.Data.SQLite.SQLiteExceptione) { thrownewException(e.Message); } } ///<summary> ///执行查询语句,返回DataSet ///</summary> ///<paramname="SQLString">查询语句</param> ///<returns>DataSet</returns> publicstaticDataSetQuery(stringSQLString,paramsSQLiteParameter[]cmdParms) { using(SQLiteConnectionconnection=newSQLiteConnection(connectionString)) { SQLiteCommandcmd=newSQLiteCommand(); PrepareCommand(cmd,connection,null,SQLString,cmdParms); using(SQLiteDataAdapterda=newSQLiteDataAdapter(cmd)) { DataSetds=newDataSet(); try { da.Fill(ds,"ds"); cmd.Parameters.Clear(); } catch(System.Data.SQLite.SQLiteExceptionex) { thrownewException(ex.Message); } returnds; } } } privatestaticvoidPrepareCommand(SQLiteCommandcmd,SQLiteConnectionconn,SQLiteTransactiontrans,stringcmdText,SQLiteParameter[]cmdParms) { if(conn.State!=ConnectionState.Open) conn.Open(); cmd.Connection=conn; cmd.CommandText=cmdText; if(trans!=null) cmd.Transaction=trans; cmd.CommandType=CommandType.Text;//cmdType; if(cmdParms!=null) { foreach(SQLiteParameterparmincmdParms) cmd.Parameters.Add(parm); } } #endregion } }
希望本文所述对大家的C#程序设计有所帮助。