• C# 执行多条SQL语句,实现数据库事务(通过Hashtable存储数据) .


    1)Web.config中添加连接数据库的语句

    <add key="ConnectionString" value="server=./sqlexpress;database=transactionDemo;uid=sa;pwd=123456"/>

    2)在表现层Default.aspx.cs中添加多条需要插入数据库的数据。

    public void Add()
            {
                List<Maticsoft.Model.ClassDemo> modellist = new List<Maticsoft.Model.ClassDemo>();
                modellist.Add(new Maticsoft.Model.ClassDemo("张三1", "浙江1"));
                modellist.Add(new Maticsoft.Model.ClassDemo("张三2", "浙江2"));
                modellist.Add(new Maticsoft.Model.ClassDemo("张三3张三3张三3张三3", "浙江3"));
                bll.Add(modellist);
            }

    3)在业务逻辑层ClassDemo.cs中添加业务逻辑

            public void Add(List<Maticsoft.Model.ClassDemo> modellist)
            {
                dal.Add(modellist);
            }

    4)在数据访问层ClassDemo.cs、DbHelperSQL.cs中添加将数据插入数据库

    /// <summary>
            /// 通过事务添加多条数据
            /// </summary>
            /// <param name="modellist"></param>
            public void Add(List<Maticsoft.Model.ClassDemo> modellist)
            {
                Hashtable SQLStringList = new Hashtable();
                for (int i = 0; i < modellist.Count;i++ )
                {
                    SqlParameter[] parameters = {
         new SqlParameter(string.Format("@name{0}",i), SqlDbType.NVarChar,50),
         new SqlParameter(string.Format("@address{0}",i), SqlDbType.NVarChar,50)};
                    parameters[0].Value = modellist[i].name;
                    parameters[1].Value = modellist[i].address;
                    SQLStringList.Add(string.Format("insert into ClassDemo (name,address) values (@name{0},@address{0})", i), parameters);
                }
                DbHelperSQL.ExecuteSqlTran(SQLStringList);
            }

     public static string connectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString();  

    /// <summary>
            /// 执行多条SQL语句,实现数据库事务。
            /// </summary>
            /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
            public static void ExecuteSqlTran(Hashtable SQLStringList)
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        SqlCommand cmd = new SqlCommand();
                        try
                        {
                            //循环
                            foreach (DictionaryEntry myDE in SQLStringList)
                            {
                                string cmdText = myDE.Key.ToString();
                                SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
                                PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
                                int val = cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                            }
                            trans.Commit();
                        }
                        catch(Exception ex)
                        {
                            trans.Rollback();
                        }
                    }
                }
            }

  • 相关阅读:
    Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法
    Oracle用imp导入dmp 提示遇到 ORACLE 错误 12560 TNS: 协议适配器错误 解决方法
    Oracle恢复误删除表操作语句
    DevExpress GridControl使用方法总结
    PL/SQL Developer 中的问题:Initialization error Could not load ".../oci.dll"解决方法
    Oracle中查询当前数据库中的所有表空间和对应的数据文件语句命令
    [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现)
    [iOS]ios archives 出现的是other items而不是iOS Apps的解决方案
    [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(二.配置MySQL数据库)
    [软件]XAMPP错误解决
  • 原文地址:https://www.cnblogs.com/leischen/p/2293939.html
Copyright © 2020-2023  润新知