程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#數據庫事務處理

C#數據庫事務處理

編輯:C#基礎知識

前言:

      通常SqlHelper類為了方便處理,做成了靜態類,靜態類的問題是不方便添加事務處理。

      實例化類方便添加事務處理,DoTrans/CommitTrans/RollBackTrans  三個函數

 

說明:

1:ExecuteNonQuery執行多條SQL語句,默認包含事務。

實際執行代碼:

SqlServerInfo ssi = new SqlServerInfo();
            string strSql="UPDATE dbo.Test SET testname='2321' WHERE testid=1;UPDATE dbo.TestCustorm SET TestNickName='Nick2321' WHERE testid=1;";
            //string strsql1 = "UPDATE dbo.TestCustorm SET TestNickName='Nick2321' WHERE testid=1;";
            //string strsql2 = "UPDATE dbo.Test SET testname='2321' WHERE testid=1";
            int i=ssi.ExecuteNonQuerySqlTextWithNoTrans(strSql);
            if(i>0)
            {
                Response.Write("執行成功");
            }
            else
            {
                Response.Write("執行失敗");
            }

 

SQL執行代碼:

public class SqlServerInfo
    {
        private string _SqlConnectionString = "Data Source=(local);Initial Catalog=test;User ID=sa;Password=sasa;";
        public string SqlConnectionString
        {
            get
            {
                return _SqlConnectionString;
            }
            set
            {
                _SqlConnectionString = value;
            }
        }

        /// <summary>
        /// 執行sql語句並返回受影響行數
        /// </summary>
        /// <param name="cmdText">sql語句</param>
        /// <returns></returns>
        public int ExecuteNonQuerySqlTextWithTrans(string cmdText)
        {
            int num2=0;
            SqlConnection connection = new SqlConnection(_SqlConnectionString);
            connection.Open();
            SqlCommand cmd = new SqlCommand();
            SqlTransaction sTran = connection.BeginTransaction();
            try
            {
                PrepareCommand(cmd, connection, sTran, CommandType.Text, cmdText, null);
                num2 = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                sTran.Commit();
                connection.Close();

            }
            catch (Exception ex)
            {
                //LogHelper log = new LogHelper();
                //log.WriteLog("

2:分割執行包含事務

執行語句:

protected void Button1_Click(object sender, EventArgs e)
        {
            SqlServerInfo ssi = new SqlServerInfo();
            int iSeed = 3;
            Random ran = new Random(iSeed);
            int RandKey=ran.Next(100,999);
            string strSql = "UPDATE dbo.Test SET testname='" + RandKey.ToString() + "' WHERE testid=1;UPDATE dbo.TestCustorm SET TestNickName1='Nick" + RandKey.ToString() + "' WHERE testid=1";
            //string strsql1 = "UPDATE dbo.TestCustorm SET TestNickName='Nick2321' WHERE testid=1;";
            //string strsql2 = "UPDATE dbo.Test SET testname='2321' WHERE testid=1";
            int i=ssi.ExecuteNonQuerySqlTextWithNoTrans(strSql);
            if(i>0)
            {
                Response.Write("執行成功");
            }
            else
            {
                Response.Write("執行失敗");
            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SqlServerInfo ssi = new SqlServerInfo();
            int iSeed = 4;
            Random ran = new Random(iSeed);
            int RandKey = ran.Next(1000, 9999);
            string strSql = "UPDATE dbo.Test SET testname='" + RandKey.ToString() + "' WHERE testid=1;UPDATE dbo.TestCustorm SET TestNickName1='Nick" + RandKey.ToString() + "' WHERE testid=1";
            //string strsql1 = "UPDATE dbo.TestCustorm SET TestNickName='Nick2321' WHERE testid=1;";
            //string strsql2 = "UPDATE dbo.Test SET testname='2321' WHERE testid=1";
            int i = ssi.ExecuteNonQuerySqlTextWithTrans(strSql);
            if (i > 0)
            {
                Response.Write("執行成功");
            }
            else
            {
                Response.Write("執行失敗");
            }

        }

處理代碼:

public class SqlServerInfo
    {
        private string _SqlConnectionString = "Data Source=(local);Initial Catalog=test;User ID=sa;Password=sasa;";
        public string SqlConnectionString
        {
            get
            {
                return _SqlConnectionString;
            }
            set
            {
                _SqlConnectionString = value;
            }
        }

        /// <summary>
        /// 執行sql語句並返回受影響行數
        /// </summary>
        /// <param name="cmdText">sql語句</param>
        /// <returns></returns>
        public int ExecuteNonQuerySqlTextWithTrans(string cmdText)
        {
            int num2=0;
            SqlConnection connection = new SqlConnection(_SqlConnectionString);
            connection.Open();
            SqlCommand cmd = new SqlCommand();
            SqlTransaction sTran = connection.BeginTransaction();
            try
            {
                string[] sqlContexts= cmdText.Split(';');
                foreach(string sql in sqlContexts)
                {
                    PrepareCommand(cmd, connection, sTran, CommandType.Text, cmdText, null);
                    num2 = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                }
                sTran.Commit();
                connection.Close();

            }
            catch (Exception ex)
            {
                //LogHelper log = new LogHelper();
                //log.WriteLog("

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved