zoukankan      html  css  js  c++  java
  • .Net插入大批量数据

     1、 使用SqlDataAdapter

            /// <summary>
            /// 实现数据库事务,大批量新增数据
            /// </summary>
            /// <param name="dt">数据源</param>
            public static int ExecuteSqlTran(DataTable dt)
            {
                int affectRows = 0;
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand())
                    {
                        SqlTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
                        try
                        {
                            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();

                            string insertcmd = @"
                                            DECLARE @Count INT;
                                            SELECT @Count = COUNT(*) FROM {0} WHERE {3};
                                            IF(@Count = 0)
                                            INSERT INTO {0} ({1}) VALUES ({2})";

                            StringBuilder columns = new StringBuilder();
                            StringBuilder filters = new StringBuilder();
                            StringBuilder paramvalues = new StringBuilder();

                            foreach (DataColumn dc in dt.Columns)
                            {
                                columns.Append(dc.ColumnName + ",");
                                paramvalues.Append("@" + dc.ColumnName + ",");
                                filters.AppendFormat(" {0} = @{0} AND ", dc.ColumnName);
                                SqlParameter par = new SqlParameter();
                                par.ParameterName = "@" + dc.ColumnName;
                                par.SourceColumn = dc.ColumnName;
                                command.Parameters.Add(par);
                            }
                            string column = columns.ToString().Substring(0, columns.ToString().LastIndexOf(','));
                            string paramvalue = paramvalues.ToString().Substring(0, paramvalues.ToString().LastIndexOf(','));
                            string filter = filters.ToString().Substring(0, filters.ToString().LastIndexOf("AND"));
                            command.CommandText = string.Format(insertcmd, dt.TableName, column, paramvalue, filter);
                            command.Connection = connection;
                            command.Transaction = transaction;
                            sqlDataAdapter.InsertCommand = command;

                            affectRows = sqlDataAdapter.Update(dt);

                            transaction.Commit();
                            connection.Close();
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            throw;
                        }
                        finally
                        {
                            command.Dispose();
                            connection.Close();
                        }
                    }
                }
                return affectRows;
            }

  • 相关阅读:
    面试题47题
    深度学习面试
    神经网络训练中的梯度消失与梯度爆炸
    Softmax函数与交叉熵
    sourceTree 添加 ssh key 方法
    request.form()和request()的区别
    C#中Request.ServerVariables详细说明及代理
    Page_Load事件与IsPostBack属性
    CSS中position的absolute和relative用法
    读取游标
  • 原文地址:https://www.cnblogs.com/johnblogs/p/6744172.html
Copyright © 2011-2022 走看看