zoukankan      html  css  js  c++  java
  • ADO执行事务

    在工作中遇到,需要批量提交的。在sql2008以后有表变量定义,可以实现。但个人比较习惯用C#,就有下面代码,直接上代码。。。

    using (SqlConnection conn = new SqlConnection(Conn.Connection))
    {
    conn.Open();
    SqlCommand comm = new SqlCommand() { Connection = conn };
    SqlTransaction tran = conn.BeginTransaction();
    comm.Transaction = tran;
    try
    {
    comm.CommandType = CommandType.StoredProcedure;
    comm.CommandText = "tb_role_INSERT";
    SqlParameter[] parms = new SqlParameter[] {
    new SqlParameter {ParameterName="@code",Value=array[0].ToString(),SqlDbType=SqlDbType.NVarChar, Size = 100 },
    new SqlParameter {ParameterName="@name",Value=array[1].ToString(),SqlDbType=SqlDbType.NVarChar, Size = 500 },
    new SqlParameter {ParameterName="@remark",Value=array[2].ToString(),SqlDbType=SqlDbType.NText},
    new SqlParameter {ParameterName="@create_time",Value=DateTime.Now,SqlDbType=SqlDbType.DateTime},
    new SqlParameter {ParameterName="@create_user_id",Value=Lib.ReadCookie("UserID"),SqlDbType=SqlDbType.Int},
    new SqlParameter {ParameterName="@is_active",Value=1,SqlDbType=SqlDbType.Int},
    new SqlParameter {ParameterName="@manufacturer_id",Value=Lib.ReadCookie("ManuID"),SqlDbType=SqlDbType.Int},
    new SqlParameter {ParameterName="@custid",Value=Lib.ReadCookie("CustID"),SqlDbType=SqlDbType.Int},
    new SqlParameter{ParameterName="@tb_roleID",Direction=ParameterDirection.Output,SqlDbType=SqlDbType.Int},
    };
    comm.Parameters.AddRange(parms);
    comm.ExecuteNonQuery();
    int id = Convert.ToInt32(comm.Parameters["@tb_roleID"].Value);

    ArrayList sqlList = GetRolePermissionList(id, array[7].ToString());
    for (int i = 0; i < sqlList.Count; i++)
    {
    string str2 = sqlList[i].ToString();
    if (str2.Trim().Length > 1)
    {
    comm.CommandType = CommandType.Text;
    comm.CommandText = str2;
    comm.ExecuteNonQuery();
    }
    }

    tran.Commit();
    }
    catch (SqlException ex)
    {
    tran.Rollback();
    messge = ex.Message;
    }
    }

  • 相关阅读:
    redis系列之1----redis简介以及linux上的安装
    springboot利用redis实现分布式锁(redis为单机模式)
    tomcat多个springboot项目启动失败
    rabbitmq启动时出错epmd error for host
    rabbitmq安装
    redis关闭报没有权限No auth
    rabbitmq添加自启动 centos7环境
    springboot rabbitmq 找不到队列
    MyBatis常用标签
    vue 强制刷新组件重新渲染
  • 原文地址:https://www.cnblogs.com/zhuyapeng/p/5665747.html
Copyright © 2011-2022 走看看