首页 > 其他 > 详细

批量插入数据(1)

时间:2016-12-18 20:32:55      阅读:141      评论:0      收藏:0      [点我收藏+]

  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 = "T_EIInformation";//要插入的表的表明
                        bulkCopy.ColumnMappings.Add("Email", "Email");//映射字段名 DataTable列名 ,数据库 对应的列名
                        bulkCopy.ColumnMappings.Add("author", "author");
                        bulkCopy.ColumnMappings.Add("Title", "Title");
                        bulkCopy.ColumnMappings.Add("Type", "Type");
                        bulkCopy.ColumnMappings.Add("confName", "confName");
                        bulkCopy.ColumnMappings.Add("Language", "Language");
                        bulkCopy.ColumnMappings.Add("Publicationyear", "Publicationyear");
                        bulkCopy.ColumnMappings.Add("Conferencelocation", "Conferencelocation");
                        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.
                      
                    }
                }

            }
           
        }

批量插入数据(1)

原文:http://www.cnblogs.com/nxxshxf/p/6195346.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!