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