您的位置:首页 > 数据库

(C#)执行多条SQL语句,事务处理

2008-09-07 16:57 716 查看
///

   /// 执行多条SQL语句,事务处理

   ///

   /// 以";"相隔的查询语句

   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
     {

      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;

   }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: