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

posted @ 2005-08-31 00:46  浪剑  阅读(1709)  评论(0编辑  收藏  举报