<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        asp.net 數據庫連接類代碼(SQL)

        來源:懂視網 責編:小采 時間:2020-11-27 22:43:30
        文檔

        asp.net 數據庫連接類代碼(SQL)

        asp.net 數據庫連接類代碼(SQL): 代碼如下:public class SqlOperation { #region 屬性 /// <summary> /// 保存在Web.config中的連接字符串 /// </summary> protected static string connectionstring = System.Configurati
        推薦度:
        導讀asp.net 數據庫連接類代碼(SQL): 代碼如下:public class SqlOperation { #region 屬性 /// <summary> /// 保存在Web.config中的連接字符串 /// </summary> protected static string connectionstring = System.Configurati

        代碼如下:
        public class SqlOperation
        {
        #region 屬性
        /// <summary>
        /// 保存在Web.config中的連接字符串
        /// </summary>
        protected static string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["hao"].ConnectionString;
        /// <summary>
        /// SqlConnection對象
        /// </summary>
        protected static SqlConnection conn = new SqlConnection();
        /// <summary>
        /// SqlCommand對象
        /// </summary>
        protected static SqlCommand comm = new SqlCommand();
        #endregion

        #region 內部函數
        /// <summary>
        /// 打開數據庫連接
        /// </summary>
        private static void ConnectionOpen()
        {
        if (conn.State != ConnectionState.Open)
        {
        conn.Close();
        conn.ConnectionString = connectionstring;
        comm.Connection = conn;
        try
        {
        conn.Open();
        }
        catch (Exception ex)
        {
        throw new Exception(ex.Message);
        }
        }
        }

        /// <summary>
        /// 關閉數據庫連接
        /// </summary>
        private static void ConnectionClose()
        {
        conn.Close();
        conn.Dispose();
        comm.Dispose();
        }

        #endregion

        /// <summary>
        /// 執行SQL語句
        /// </summary>
        /// <param name="SqlString">要執行的SQL語句</param>
        public static void ExecuteSQL(string SqlString)
        {
        try
        {
        ConnectionOpen();
        comm.CommandType = CommandType.Text;
        comm.CommandText = SqlString;
        comm.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
        try
        {
        ConnectionClose();
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
        throw new Exception(ex.Message);
        }
        finally
        {
        ConnectionClose();
        }
        }

        /// <summary>
        /// 執行存儲過程
        /// </summary>
        /// <param name="ProcedureName">存儲過程名稱</param>
        /// <param name="coll">存儲過程需要的參數集合</param>
        public static void ExecuteProcedure(string ProcedureName, params SqlParameter[] coll)
        {
        try
        {
        ConnectionOpen();
        comm.CommandType = CommandType.StoredProcedure;
        comm.CommandText = ProcedureName;
        comm.Parameters.Clear();
        for (int i = 0; i < coll.Length; i++)
        {
        comm.Parameters.Add(coll[i]);
        }
        comm.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
        try
        {
        ConnectionClose();
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
        throw new Exception(ex.Message);
        }
        finally
        {
        ConnectionClose();
        }
        }

        /// <summary>
        /// 執行Sql查詢并返回第一行的第一條記錄,返回object,使用時需要拆箱 -> unbox
        /// </summary>
        /// <param name="sqlstr">傳入的Sql語句</param>
        /// <returns>返回object類型的第一行第一條記錄</returns>
        public static object ExecuteScalar(string SqlString)
        {
        object obj = new object();
        try
        {
        ConnectionOpen();
        comm.CommandType = CommandType.Text;
        comm.CommandText = SqlString;
        obj = comm.ExecuteScalar();
        }
        catch (Exception ex)
        {
        try
        {
        ConnectionClose();
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
        throw new Exception(ex.Message);
        }
        finally
        {
        ConnectionClose();
        }
        return obj;
        }

        /// <summary>
        /// 執行SQL語句,同時進行事務處理
        /// </summary>
        /// <param name="sqlstr">要執行的SQL語句</param>
        public static void ExecuteTransactionSQL(string SqlString)
        {
        SqlTransaction trans;
        trans = conn.BeginTransaction();
        comm.Transaction = trans;
        try
        {
        ConnectionOpen();
        comm.CommandType = CommandType.Text;
        comm.CommandText = SqlString;
        comm.ExecuteNonQuery();
        trans.Commit();
        }
        catch (Exception ex)
        {
        try
        {
        ConnectionClose();
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
        throw new Exception(ex.Message);
        }
        finally
        {
        ConnectionClose();
        }
        }

        /// <summary>
        /// 執行指定SQL查詢,返回DataSet
        /// </summary>
        /// <param name="sqlstr">要執行的SQL語句</param>
        /// <returns>DataSet</returns>
        public static DataSet GetDataSetBySQL(string SqlString)
        {
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        try
        {
        ConnectionOpen();
        comm.CommandType = CommandType.Text;
        comm.CommandText = SqlString;
        da.SelectCommand = comm;
        da.Fill(ds);
        }
        catch (Exception ex)
        {
        try
        {
        ConnectionClose();
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
        throw new Exception(ex.Message);
        }
        finally
        {
        ConnectionClose();
        }
        return ds;
        }

        /// <summary>
        /// 通過存儲過程返回DataSet
        /// </summary>
        /// <param name="ProcedureName">存儲過程名稱</param>
        /// <param name="coll">SqlParameter集合</param>
        /// <returns>DataSet</returns>
        public static DataSet GetDataSetByProcedure(string ProcedureName, params SqlParameter[] coll)
        {
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        try
        {
        ConnectionOpen();
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        for (int i = 0; i < coll.Length; i++)
        {
        comm.Parameters.Add(coll[i]);
        }
        comm.CommandText = ProcedureName;
        da.SelectCommand = comm;
        da.Fill(ds);
        }
        catch (Exception ex)
        {
        try
        {
        ConnectionClose();
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
        throw new Exception(ex.Message);
        }
        finally
        {
        ConnectionClose();
        }
        return ds;
        }


        /// <summary>
        /// 通過存儲過程返回DataSet
        /// </summary>
        /// <param name="ProcedureName">存儲過程名稱</param>
        /// <returns>DataSet</returns>
        public static DataSet GetDataSetByProcedure(string ProcedureName)
        {
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        try
        {
        ConnectionOpen();
        comm.CommandType = CommandType.StoredProcedure;
        comm.CommandText = ProcedureName;
        comm.Parameters.Clear();
        da.SelectCommand = comm;
        da.Fill(ds);
        }
        catch (Exception ex)
        {
        try
        {
        ConnectionClose();
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
        throw new Exception(ex.Message);
        }
        finally
        {
        ConnectionClose();
        }
        return ds;
        }

        /// <summary>
        /// 返回指定sql語句的DataTable
        /// </summary>
        /// <param name="sqlstr">傳入的Sql語句</param>
        /// <returns>DataTable</returns>
        public static DataTable GetDataTableBySQL(string SqlString)
        {
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        try
        {
        ConnectionOpen();
        comm.CommandType = CommandType.Text;
        comm.CommandText = SqlString;
        da.SelectCommand = comm;
        da.Fill(dt);
        }
        catch (Exception ex)
        {
        try
        {
        ConnectionClose();
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
        throw new Exception(ex.Message);
        }
        finally
        {
        ConnectionClose();
        }
        return dt;
        }

        /// <summary>
        /// 根據存儲過程返回DataTable
        /// </summary>
        /// <param name="ProcedureName">存儲過程名</param>
        /// <param name="coll">SqlParameter集合</param>
        /// <returns>DataTable</returns>
        public static DataTable GetDataTableByProcedure(string ProcedureName, params SqlParameter[] coll)
        {
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        try
        {
        ConnectionOpen();
        comm.Parameters.Clear();
        comm.CommandType = CommandType.StoredProcedure;
        comm.CommandText = ProcedureName;
        for (int i = 0; i < coll.Length; i++)
        {
        comm.Parameters.Add(coll[i]);
        }
        da.SelectCommand = comm;
        da.Fill(dt);
        }
        catch (Exception ex)
        {
        try
        {
        ConnectionClose();
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
        throw new Exception(ex.Message);
        }
        finally
        {
        ConnectionClose();
        }
        return dt;
        }

        /// <summary>
        /// 根據存儲過程返回DataTable
        /// </summary>
        /// <param name="ProcedureName">存儲過程名稱</param>
        /// <returns>DataTable</returns>
        public static DataTable GetDataTableByProcedure(string ProcedureName)
        {
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        try
        {
        ConnectionOpen();
        comm.Parameters.Clear();
        comm.CommandType = CommandType.StoredProcedure;
        comm.CommandText = ProcedureName;
        da.SelectCommand = comm;
        da.Fill(dt);
        }
        catch (Exception ex)
        {
        try
        {
        ConnectionClose();
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
        throw new Exception(ex.Message);
        }
        finally
        {
        ConnectionClose();
        }
        return dt;
        }
        }

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        asp.net 數據庫連接類代碼(SQL)

        asp.net 數據庫連接類代碼(SQL): 代碼如下:public class SqlOperation { #region 屬性 /// <summary> /// 保存在Web.config中的連接字符串 /// </summary> protected static string connectionstring = System.Configurati
        推薦度:
        標簽: 連接 代碼 數據庫
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产精品久久久久久久久免费| 国产大片免费天天看| 91人成网站色www免费下载| 看成年女人免费午夜视频| 成人男女网18免费视频| 亚洲偷自精品三十六区| 妞干网手机免费视频| 亚洲色偷精品一区二区三区| 麻豆成人精品国产免费| 亚洲五月午夜免费在线视频| 曰韩无码AV片免费播放不卡| 久久久久国产精品免费免费搜索 | 两个人日本免费完整版在线观看1| 久久精品国产亚洲一区二区三区| 亚洲黄片手机免费观看| 亚洲VA成无码人在线观看天堂| 久久国产精品免费专区| 亚洲福利秒拍一区二区| 成人免费视频小说| 人体大胆做受免费视频| 亚洲精品二区国产综合野狼 | 精品视频一区二区三区免费| 亚洲高清在线观看| 无码精品人妻一区二区三区免费| 亚洲av无码成人精品区| 久久免费国产视频| 亚洲欧美日韩自偷自拍| 57PAO成人国产永久免费视频| 亚洲色大成网站www永久男同| www.亚洲色图.com| 久久精品亚洲日本波多野结衣| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 亚洲精品乱码久久久久久蜜桃不卡| 久久香蕉国产线看免费| 亚洲国产精品网站在线播放| 国产亚洲日韩在线三区| 亚洲高清中文字幕免费| 亚洲日本乱码一区二区在线二产线| 天堂在线免费观看中文版| 国产成人免费ā片在线观看老同学| 亚洲制服丝袜在线播放|