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;
      }

  • 相关阅读:
    初始化项目结构
    Django基础2
    Django基础
    Linux(9~)
    Linux(8~)
    redis案例
    Jedis连接池
    Jedis入门
    redis持久化
    redis命令操作
  • 原文地址:https://www.cnblogs.com/JimZhang/p/226603.html
Copyright © 2011-2022 走看看