循环写入Insert 与 SqlBulkcopy
Posted noteswiki
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循环写入Insert 与 SqlBulkcopy相关的知识,希望对你有一定的参考价值。
/* Insert by Loop */ cmd.CommandText = "insert into BizSharedStore (BizSharedStoreId,BizSharedId,StoreCode,StartDt,EndDt) values (@BizSharedStoreId,@BizSharedId,@RuleCode);"; cmd.CommandType = CommandType.Text; foreach (var x in query_BizSharedList) { try { cmd.Parameters.Clear(); cmd.Parameters.Add("@BizSharedListId", SqlDbType.VarChar).Value = x.BizSharedListId.ToString(); cmd.Parameters.Add("@BizSharedId", SqlDbType.VarChar).Value = x.BizSharedId; cmd.Parameters.Add("@RuleCode", SqlDbType.VarChar).Value = x.Rulecode; cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show("Insert BizShardList Error:" + ex.Message); } } // End of Insert by Loop MessageBox.Show("insert into BizSharedList by Insert Loop, Successfully!");
SqlBulkcopy
using (SqlBulkCopy bulk1 = new SqlBulkCopy(conn)) { bulk1.DestinationTableName = "BizSharedList"; //bulk1.ColumnMappings.Add("BizSharedListId", "BizSharedListId"); //bulk1.ColumnMappings.Add("BizSharedId", "BizSharedId"); //bulk1.ColumnMappings.Add("RuleCode", "RuleCode"); bulk1.WriteToServer(tbl_BizSharedList); } MessageBox.Show("Bulk Insert Table: BizSharedList Successfully!");
以上是关于循环写入Insert 与 SqlBulkcopy的主要内容,如果未能解决你的问题,请参考以下文章
用NOPI将上传的EXCEL,转换得到DataTable,用SqlBulkCopy将数据写入数据库表中,配置添加默认列及值,对应数据库字段写入数据