zoukankan      html  css  js  c++  java
  • IBatis 批量插入数据之SqlBulkCopy

    public void AddLetters(IList<int> customerIds, string title, string content, LetterEnum.LetterType type)
            {
                // 插入内容,获取内容ID
                var msgContentId = this.AddLetterContent(title, content, 2);
    
                // 站内信接收者表字段初始化
                var letterDataTable = this.InitDataTable();
                foreach (var customerId in customerIds)
                {
                    var letterDataRow = letterDataTable.NewRow();
                
                    letterDataRow["CustomerId"] = customerId;
    
                    letterDataRow["MsgContentId"] = msgContentId;
    
                    letterDataTable.Rows.Add(letterDataRow);
                }
    
                using (var conn = new SqlConnection(_sqlMapper.DataSource.ConnectionString))
                {
                    conn.Open();
    
                    using (var sqlBulkCopy = new SqlBulkCopy(conn))
                    {
                        sqlBulkCopy.DestinationTableName = "Letter";
                        sqlBulkCopy.BatchSize = letterDataTable.Rows.Count;
    
                        if (letterDataTable.Rows.Count > 0)
                        {
                            sqlBulkCopy.WriteToServer(letterDataTable);
                        }
                    }
                }
            }
    
            /// <summary>
            /// 初始化Letter表结构
            /// </summary>
            private DataTable InitDataTable()
            {
                var dataTable = new DataTable();
    
                using (var conn = new SqlConnection(_sqlMapper.DataSource.ConnectionString))
                {
                    var queryString = string.Format(@"select top 0 * from {0}", "Letter");
                    var adapter = new SqlDataAdapter(queryString, conn);
                    adapter.Fill(dataTable);
                  
                }
    
                return dataTable;
            }
    

      

  • 相关阅读:
    动手动脑(类与对象作业再次提交)
    论团队(类与对象邮箱作业再次提交)
    流于形式的沟通
    加密
    string类中一些方法的使用
    StringEquals的用法
    命令行接收数字求和
    计算机思维
    SpringBoot之Callable处理异步请求
    MySQL8.0 zip版本 安装
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5992078.html
Copyright © 2011-2022 走看看