批量插入数据
Posted 守候幸福
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批量插入数据相关的知识,希望对你有一定的参考价值。
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.
}
}
}
}
以上是关于批量插入数据的主要内容,如果未能解决你的问题,请参考以下文章