zoukankan      html  css  js  c++  java
  • C# 把一个DT内的数据 批量插入到数据库里(SQL server 2005)

     private static void DataTableToSQLServer(DataTable dt)
            {
                string connectionString = "";//GetConnectionString();  
    
                using (SqlConnection destinationConnection = new SqlConnection(connectionString))
                {
                    destinationConnection.Open();
    
                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
                    {
                        try
                        {
                            bulkCopy.DestinationTableName = "table_name";//要插入的表的表明   
                            bulkCopy.ColumnMappings.Add("feild1", "feild1");//映射字段名 DataTable列名 ,数据库 对应的列名   
                            bulkCopy.ColumnMappings.Add("feild2", "feild2");
                            /*...
                             * ...
                             * ...
                             * ...
                             * ...*/
                            bulkCopy.WriteToServer(dt);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        finally
                        {
                            /* Close the SqlDataReader. The SqlBulkCopy   
                             object is automatically closed at the end   
                             of the using block.*/
                        }
                    }
                }
            }
  • 相关阅读:
    Java 测试代码模板
    git 保存用户名和密码
    git 高级命令
    git 最常用命令
    git 冲突解决
    git diff命令
    nginx静态服务器的配置
    使用SFTP工具下载文件
    git log 格式化输出
    9-angular.fromJson
  • 原文地址:https://www.cnblogs.com/jcdd-4041/p/3392352.html
Copyright © 2011-2022 走看看