批量插入数据 C# SqlBulkCopy使用
转自:http://blog.csdn.net/wangzh300/article/details/7382506
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. } } } }
作者:xuejianxiyang
出处:http://xuejianxiyang.cnblogs.com
关于作者:Heaven helps those who help themselves.
本文版权归原作者和博客园共有,欢迎转载,但未经原作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。