zoukankan      html  css  js  c++  java
  • C#使用事务

            

             /// <summary>
            /// 执行多条SQL语句,实现数据库事务。
            /// </summary>
            /// <param name="SQLStringList">多条SQL语句</param>  
            public static int ExecuteSqlTran(List<String> SQLStringList)
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = connection;
                    SqlTransaction tx = connection.BeginTransaction();
                    cmd.Transaction = tx;
                    try
                    {
                        int count = 0;
                        for (int n = 0; n < SQLStringList.Count; n++)
                        {
                            string strsql = SQLStringList[n];
                            if (strsql.Trim().Length > 1)
                            {
                                cmd.CommandText = strsql;
                                count += cmd.ExecuteNonQuery();
                            }
                        }
                        tx.Commit();
                        return count;
                    }
                    catch
                    {
                        tx.Rollback();
                        return 0;
                    }
                    finally
                    {
                        cmd.Dispose();
                        connection.Close();
                    }
                }
            }

  • 相关阅读:
    STM32(五)固件库文件分析
    STM32(四)GPIO初始化顺序
    STM32(三)GPIO的8种工作模式介绍
    date命令详解
    "System.Security.Cryptography.CryptographicException: 拒绝访问" 问题的解决方法
    记录一次win2003服务器的IIS服务加载.flv后缀的资源报错404的处理方法
    Linux之修改主机名(永久生效)
    Python3 isdigit()方法
    Python3 字典 items() 方法
    Linux shell脚本中shift的用法说明
  • 原文地址:https://www.cnblogs.com/paste/p/1969982.html
Copyright © 2011-2022 走看看