SqlBulkCopy
Posted noteswiki
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SqlBulkCopy相关的知识,希望对你有一定的参考价值。
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. } } } }
以上是关于SqlBulkCopy的主要内容,如果未能解决你的问题,请参考以下文章
导入excel把数据存在datatable中,用sqlbulkcopy把datable批量导入到数据库的表中
如何使用 SqlBulkCopy 并通过 Glimpse 跟踪批量插入