zoukankan      html  css  js  c++  java
  • 执行多条SQL语句,事务处理

      /// <summary>
      /// 执行多条SQL语句,事务处理
      /// </summary>
      /// <param name="strSQL">以";"相隔的查询语句</param>
      public bool InsertMoreDB(string strSQL){
       char[] de={';'};
       string strChildSQL;
       int i;
       string[] strSQLArr=strSQL.Split(de);
       bool IsControll=true;

       SqlConnection conn=DBCreateCN();
       SqlCommand myCommand=new SqlCommand();
       SqlTransaction myTrans;
       myTrans=conn.BeginTransaction();

       myCommand.Connection = conn;
       myCommand.Transaction = myTrans;

       try
       {
        for(i=0;i<strSQLArr.Length;i++)
        {
         strChildSQL=strSQLArr[i];
         myCommand.CommandText =strChildSQL;
         myCommand.ExecuteNonQuery();
        }
        myTrans.Commit();
        IsControll=true;
       }
       catch(Exception)
       {
        try
        {
         IsControll=false;
         myTrans.Rollback();

        }
        catch (SqlException)
        {
         // Handle possible exception here
        }
       }
       finally
       {
        conn.Close();
       }
       return IsControll;
      }

  • 相关阅读:
    2020.7.23第十八天
    2020.7.22第十七天
    本周总结
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
  • 原文地址:https://www.cnblogs.com/JimZhang/p/226603.html
Copyright © 2011-2022 走看看