执行多条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;
  }

原文地址:https://www.cnblogs.com/JimZhang/p/226603.html