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

  • 相关阅读:
    [Java]关于throw,throws,try{}catch(){} 悟寰轩
    jmx使用jmxmp协议连接器的实现 悟寰轩
    ActionContextCleanUp作用 悟寰轩
    空心验证码(定制) 悟寰轩
    struts升级:FileUploadInterceptor在struts 2.3.14.2的jar中修改了方法acceptFile中的参数 悟寰轩
    网络里的“逆世界” 悟寰轩
    离开时自动提示设为首页
    文本与图像上传到数据库
    在Delphi用vbscript的正则表达式
    ASP中输入特殊字符
  • 原文地址:https://www.cnblogs.com/paste/p/1969982.html
Copyright © 2011-2022 走看看