我是靠谱客的博主 细心冬天,这篇文章主要介绍执行多条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/archive/2005/08/31/226603.html

最后

以上就是细心冬天最近收集整理的关于执行多条SQL语句,事务处理的全部内容,更多相关执行多条SQL语句内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部