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

          /// <summary>
            /// 批量插入  注:DT的tableName为要更新的数据库表名,DT的列名和数据库一致
            /// </summary>
            /// <param name="dt"></param>
            /// <remarks></remarks>
            public void  tsSqlBulkCopy(DataTable dt)
            {
    
                try
                {
     
                    using (SqlBulkCopy sqlCopy = new SqlBulkCopy(this.connection, SqlBulkCopyOptions.FireTriggers, this.transaction))
                    {
    
                        string sql = "select * from " + dt.TableName + " where 1=-1";
                        DataTable DBTable = ExecuteDataTable(sql);
                        List<string> DBList = new List<string>();
    
                        for (int i = 0; i <= DBTable.Columns.Count - 1; i++)
                        {
                            DBList.Add(DBTable.Columns[i].ColumnName);
                        }
    
                        List<string> ParList = new List<string>();
                        for (int i = 0; i <= dt.Columns.Count - 1; i++)
                        {
                            ParList.Add(dt.Columns[i].ColumnName);
                        }
    
                        //添加对应列
    
                        foreach (string item in DBList)
                        {
                            if (ParList.Contains(item))
                            {
                                sqlCopy.ColumnMappings.Add(item, item);
                            }
                        }
    
    
                        sqlCopy.DestinationTableName = dt.TableName;
                        sqlCopy.BatchSize = dt.Rows.Count;
                        sqlCopy.WriteToServer(dt);
             
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    
            }
  • 相关阅读:
    Makefile 之 $(Q)
    LeetCode-50-Pow(x, n)
    LeetCode-49. Group Anagrams
    全排列问题全面解析
    LeetCode-47. Permutations II
    LeetCode-46. Permutations
    LeetCode-43. Multiply Strings
    LeetCode-40. Combination Sum II
    LeetCode-39. Combination Sum
    LeetCode-36. Valid Sudoku
  • 原文地址:https://www.cnblogs.com/longling2344/p/5445383.html
Copyright © 2011-2022 走看看