C# 實現的MSSQL 訪問類(MSSQLHelper)
阿新 • • 發佈:2019-01-28
資料庫操作是軟體的核心之一,網上也有很多關於資料庫操作的文章,並附有很多優秀的原始碼。之前寫了一個系統,留下很多很多有用的資源,現在整理出訪問SQL Server的C#實現類,算是對之前的工作做一個總結,對自己有用,也希望對其他人有用!歡迎提出寶貴意見!
MSSQLHelper 資料庫訪問類:
欄位 (私有):
SqlConnection 連線例項
SqlCommand 命令例項
SqlDataAdapter 資料介面卡
String 資料庫 驅動連線字串
方法:
(1)建構函式;
(2)ExecuteNonQuery 執行儲存過程或SQL指令碼,返回執行狀態
(3)ExecuteNonQueryWithReturn 執行儲存過程,並返回儲存過程的返回值 (Return)
(4)ExecuteQuery 執行儲存過程,並返回結果集
(5)BeginExecuteQuery 非同步執行儲存過程成功返回1,否則-1
(5)CallBackExecuteNonQuery 非同步執行儲存過程回撥函式
注:因為經過很多改動,改動後可能有些潛在bug,若發現歡迎指正!
/* Author : YEZ Date : 2014/12/9 Discribe : visitor for MSSQL Server Database. */ namespace lib_ez { using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Configuration; public class MSSQLHelper { private SqlCommand comm; private SqlConnection conn; private SqlDataAdapter adapter; private String connString; public MSSQLHelper() { this.connString = ConfigurationManager.AppSettings["sqlConnString"]; this.conn = new SqlConnection (this.connString); this.comm = new SqlCommand (); this.comm.Connection = this.conn; this.adapter = new SqlDataAdapter (); } /// <summary> /// 執行帶引數或不帶引數的sql語句或儲存過程,返回執行狀態 /// </summary> /// <param name="sqlStr">sql語句</param> /// <param name="type">執行型別</param> /// <param name="paras">預設數量的引數</param> /// <returns>返回資料庫執行狀態碼</returns> public int ExecuteNonQuery(string _sql, CommandType _type, params SqlParameter[] _paras) { int res = -1; this.comm.Parameters.Clear (); if (_paras != null) this.comm.Parameters.AddRange (_paras); this.comm.CommandText = _sql; this.comm.CommandType = _type; try { this.conn.Open (); res = comm.ExecuteNonQuery (); return res; } catch (Exception ex) { // throw all onto upper layer return res; } finally { conn.Close (); } } /// <summary> /// 執行帶引數或不帶引數的儲存過程,返回儲存過程中的int型返回值 /// </summary> /// <param name="sqlStr">儲存過程名</param> /// <param name="paras">任意多的引數</param> /// <returns></returns> public int ExecuteNonQueryWithReturn(string _sql, params SqlParameter[] _paras) { // add return parameter SqlParameter rtn = new SqlParameter (); rtn.Direction = ParameterDirection.ReturnValue; this.comm.Parameters.Clear (); if (_paras != null) this.comm.Parameters.AddRange (_paras); this.comm.Parameters.Add (rtn); this.comm.CommandText = _sql; this.comm.CommandType = CommandType.StoredProcedure; try { conn.Open(); comm.ExecuteNonQuery(); return int.Parse(rtn.Value.ToString()); } catch (Exception ex) { // throw all onto upper layer return -1; } finally { conn.Close (); } } /// <summary> /// 非同步執行,作為靜態方法,此方法沒有測試過,慎用! /// </summary> /// <param name="sqlStr"></param> /// <param name="paras"></param> /// <returns></returns> public static int BeginExecuteQuery(string _sql, CommandType _type, params SqlParameter[] _paras) { SqlConnection _con = new SqlConnection (ConfigurationManager.AppSettings["sqlConnString"]); SqlCommand _cmd = new SqlCommand (_con); _cmd.CommandText = _sql; _cmd.CommandType = _type; _cmd.Parameters.Clear (); _cmd.Parameters.AddRange(_paras); try { _con.Open (); // async run IAsyncResult asyncResult = _cmd.BeginExecuteNonQuery (MSSQLHelper.CallBackExecuteNonQuery, _cmd); return 1; // indicate as an success in call the fxn } catch (Exception ex) { // throw all onto upper layer return -1; } finally { if (_con != null) { _con.Close (); _con.Dispose (); _cmd.Dispose (); } } } /// <summary> /// 非同步執行回撥函式,用於傳給command物件的非同步回撥代理 /// </summary> /// <param name="callBack"></param> protected static void CallBackExecuteNonQuery(IAsyncResult _call_back) { SqlCommand _cmd = null; try { _cmd = (SqlCommand) _call_back.AsyncState; if (_cmd != null) _cmd.EndExecuteNonQuery (_call_back); return; } catch (Exception ex) { } finally { //關閉相關的實體 if (_cmd != null && _cmd.Connection != null) { _cmd.Connection.Close(); _cmd.Dispose(); } } } /// <summary> /// 執行並返回結果集 /// </summary> /// <param name="sqlStr">儲存過程名稱或結果集</param> /// <param name="ct">命令形式</param> /// <param name="paras">引數型別</param> /// <returns>結果集</returns> public DataTable ExecuteQuery(string _sql, CommandType _type, params SqlParameter[] _paras) { DataTable dset = null; this.comm.Parameters.Clear (); if (_paras != null) this.comm.Parameters.AddRange(_paras); this.comm.CommandText = _sql; this.comm.CommandType = _type; this.adapter.SelectCommand = comm; try { conn.Open(); adapter.Fill(dset); return dset; } catch (Exception ex) { return dset; } finally { conn.Close(); } } } }