zoukankan      html  css  js  c++  java
  • C# 利用DataTable批处理数据导入数据库

    public static void DataTableToSQLServer(DataTable dt, string connectString, string tableName)
              {
                  string connectionString = connectString;
     
                  using (SqlConnection destinationConnection = new SqlConnection(connectionString))
                  {
                      destinationConnection.Open();
     
                      using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
                     {
                         try
                         {
                             bulkCopy.DestinationTableName = tableName;//要插入的表的表名
                             bulkCopy.BatchSize = dt.Rows.Count;
                             bulkCopy.ColumnMappings.Add("ID", "ID");//映射字段名 DataTable列名 ,数据库 对应的列名  
                             bulkCopy.ColumnMappings.Add("primaryDataID", "primaryDataID");
                             bulkCopy.ColumnMappings.Add("SysKeywordID", "SysKeywordID");
                             bulkCopy.ColumnMappings.Add("value", "value");
                             bulkCopy.ColumnMappings.Add("UserID", "UserID");
                             bulkCopy.ColumnMappings.Add("IsTitle", "IsTitle");
                             bulkCopy.ColumnMappings.Add("IsContent", "IsContent");
                             bulkCopy.ColumnMappings.Add("ContentCount", "ContentCount");
                             bulkCopy.ColumnMappings.Add("IsMatching", "IsMatching");
                             bulkCopy.ColumnMappings.Add("CreateDate", "CreateDate");

                            bulkCopy.WriteToServer(dt);
                        }
                         catch (Exception ex)
                         {
                             Console.WriteLine(ex.Message);
                         }
                         finally
                         {

                         }
                     }
                 }
             }
    ————————————————
    版权声明:本文为CSDN博主「seven9627」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/seven9627/java/article/details/93876235

  • 相关阅读:
    解压缩编码列表
    按既定顺序创建目标数组
    整数的各位积和之差
    好数对的数目
    拿硬币
    设计 Goal 解析器
    【求助】win 2008 R2 远程桌面多用户,破解最大连接数2的限制
    Java 字符串拼接 五种方法的性能比较分析 从执行100次到90万次
    Java abstract class 和 interface 的区别
    忘记BIOS超级管理员密码,怎么破解?
  • 原文地址:https://www.cnblogs.com/rain4138/p/13072894.html
Copyright © 2011-2022 走看看