通用数据库连接执行类(SQL)
生活随笔
收集整理的這篇文章主要介紹了
通用数据库连接执行类(SQL)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using System; using System.Data; using System.Data.SqlClient; namespace Public { ???? ///<summary> ???? /// CustomADO 數據連接執行類。 ???? ///</summary> ???? public class CustomADO ???? { ???????? #region定義或創建類私有變量或對象 ???????? private string _connstr;???????? //連接字符串存儲變量 ???????? private string _procedure;?????? //存儲過程名或數據命令字符串變量 ???????? private bool _isprocedure;????? //是否使用存儲過程 ???????? private SqlConnection _conn;???? //連接對象 ???????? private SqlCommand _comm;??????? //數據命令對象 ???????? private SqlDataReader _dr;?????? //定義數據讀取器 ???????? private DataSet _ds;???????????? //定義數據存儲器 ???????? ???????? #endregion ???????? #region構造函數 ???????? ///<summary> ???????? ///構造函數重載1 ???????? ///</summary> ???????? public CustomADO() ???????? { ????????????? _connstr = null; ????????????? _procedure = null; ????????????? _isprocedure = false; ????????????? _conn = new SqlConnection(); ????????????? _comm = new SqlCommand(); ??????? } ???????? ///<summary> ???????? ///構造函數重載2 ???????? ///</summary> ???????? ///<param name="connstring">數據庫連接字符串</param> ???????? ///<param name="procedure">存儲過程或SQL命令</param> ???????? ///<param name="isprocedure">是否使用存儲過程</param> ???????? public CustomADO(string connstring,string procedure,bool isprocedure) ???????? { ????????????? _connstr = connstring; ????????????? _procedure = procedure; ????????????? _isprocedure = isprocedure; ????????????? _conn = new SqlConnection(); ????????????? _comm = new SqlCommand(); ???????? } ???????? #endregion ???????? #region屬性 ???????? ///<summary> ???????? ///設置或獲取數據庫連接字符串 ???????? ///</summary> ???????? public string ConnString ???????? { ????????????? get ????????????? { ?????????????????? return _connstr; ????????????? } ????????????? set ????????????? { ?????????????????? _connstr = value; ?????????????????? _conn.ConnectionString = _connstr; ?????????????????? _comm.Connection = _conn; ????????????? } ???????? } ???????? ///<summary> ???????? ///設置或獲取存儲過程名或SQL命令字符串 ???????? ///</summary> ???????? public string Procedure ???????? { ????????????? get ????????????? { ?????????????????? return _procedure; ????????????? } ????????????? set ????????????? { ?????????????????? _procedure = value; ?????????????????? _comm.CommandText = _procedure; ????????????? } ???????? } ???????? ///<summary> ???????? ///設置是否存儲過程屬性 ???????? ///</summary> ???????? public bool IsProcedure ???????? { ????????????? set ????????????? { ?????????????????? _isprocedure = value; ?????????????????? if (_isprocedure) ?????????????????? { ?????????????????????? _comm.CommandType = CommandType.StoredProcedure; ?????????????????? } ?????????????????? else ?????????????????? { ?????????????????????? _comm.CommandType = CommandType.Text; ?????????????????? } ????????????? } ???????? } ???????? #endregion ???????? #region方法 ???????? ///<summary> ???????? ///打開連接 ???????? ///</summary> ???????? public void OpenConnection() ???????? { ????????????? _conn.Open(); ???????? } ???????? ///<summary> ???????? ///關閉連接和數據讀取器并清空數據集合 ???????? ///</summary> ???????? public void CloseConnection() ???????? { ????????????? _conn.Close(); ???????? } ???????? ///<summary> ???????? ///銷毀連接對象和方法 ???????? ///</summary> ???????? public void Dispost() ???????? { ????????????? if (_ds != null) ????????????? { ?????????????????? _ds.Clear(); ????????????? } ????????????? if (_dr!=null) ????????????? { ?????????????????? _dr.Close(); ????????????? } ????????????? _comm.Dispose(); ????????????? _conn.Close(); ????????????? _conn.Dispose(); ???????? } ???????? ///<summary> ???????? ///添加參數重載1 ???????? ///</summary> ???????? ///<param name="dbtype">參數類型</param> ???????? ///<param name="parametername">參數名</param> ???????? ///<param name="pvalue">參數值</param> ???????? public void AddParameter(DbType dbtype,string parametername,object pvalue) ???????? { ????????????? SqlParameter p = new SqlParameter(); ????????????? ????????????? p.DbType = dbtype; ????????????? p.ParameterName = parametername; ????????????? p.Value = pvalue; ????????????? _comm.Parameters.Add(p); ???????? } ???????? ///<summary> ???????? ///添加參數重載2 ???????? ///</summary> ???????? ///<param name="dbtype">參數類型</param> ???????? ///<param name="parametername">參數名</param> ???????? ///<param name="pvalue">參數值</param> ???????? ///<param name="parameterdirection">參數類型</param> ???????? public void AddParameter(DbType dbtype,string parametername,object pvalue,ParameterDirection parameterdirection) ???????? { ????????????? SqlParameter p = new SqlParameter(); ????????????? ????????????? p.DbType = dbtype; ????????????? p.ParameterName = parametername; ????????????? p.Value = pvalue; ????????????? p.Direction = parameterdirection; ????????????? _comm.Parameters.Add(p); ???????? } ???????? ///<summary> ???????? ///添加返回參數 ???????? ///</summary> ???????? ///<param name="dbtype">DbType參數類型</param> ???????? ///<param name="parametername">參數名</param> ???????? public void AddParameterReturnValue(DbType dbtype,string parametername) ???????? { ????????????? SqlParameter p = new SqlParameter(); ??????????? p.DbType = dbtype; ????????????? p.ParameterName = parametername; ????????????? p.Direction = ParameterDirection.ReturnValue; ????????????? _comm.Parameters.Add(p); ???????? } ???????? ///<summary> ???????? ///返回DataSet對象 ???????? ///</summary> ???????? ///<returns>執行數據命令后返回的DataSet對象</returns> ???????? public DataSet ExecuteDataSet() ???????? { ????????????? _ds = new DataSet(); ????????????? SqlDataAdapter da = new SqlDataAdapter(); ????????????? da.SelectCommand = _comm; ????????????? da.Fill(_ds); ????????????? return _ds; ???????? } ???????? ///<summary> ???????? ///返回DataReader對象 重載1 ???????? ///</summary> ???????? ///<returns>數據讀取器</returns> ???????? public SqlDataReader ExecuteDataReader() ???????? { ????????????? _dr = _comm.ExecuteReader(); ????????????? return _dr; ???????? } ???????? ///<summary> ???????? ///返回DataReader對象 重載2 ???????? ///</summary> ???????? ///<param name="behavior">CommandBehavior參數,對參數結果的數據庫影響說明</param> ???????? ///<returns>數據讀取器</returns> ???????? public SqlDataReader ExecuteDataReader(CommandBehavior behavior) ???????? { ????????????? _dr = _comm.ExecuteReader(behavior); ????????????? return _dr; ???????? } ???????? ///<summary> ???????? ///執行數據命令并返回影響行數 ???????? ///</summary> ???????? public int ExecuteNonQuery() ???????? { ???????? ???? return _comm.ExecuteNonQuery(); ???????? } ???????? ///<summary> ???????? ///通過參數名獲得參數值 ???????? ///</summary> ???????? ///<param name="parametername">參數名</param> ???????? ///<returns>返回參數值</returns> ???????? public object GetParameterValue(string parametername) ???????? { ????????????? object returnvalue; ????????????? returnvalue = _comm.Parameters[parametername].Value; ????????????? ????????????? return returnvalue; ???????? } ???????? #endregion ???? } }
轉載于:https://www.cnblogs.com/ZetaChow/archive/2006/03/28/2237454.html
總結
以上是生活随笔為你收集整理的通用数据库连接执行类(SQL)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 想你,是一种美丽
- 下一篇: [讨论]你的女朋友值多少钱?