sqlserver2008 批量插入数据
Posted 沈阳晓东
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlserver2008 批量插入数据相关的知识,希望对你有一定的参考价值。
private DataTable GetTableSchema() { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("ID",typeof(int)), new DataColumn("WORKDAY",typeof(DateTime)), new DataColumn("WORKSN",typeof(int)), new DataColumn("PLANID",typeof(int))}); return dt; } public void BatchInsert(List<GuardTour_WorkDays> workdays) { DataTable dt = GetTableSchema(); for (int i = 0; i < workdays.Count; i++) { DataRow dr = dt.NewRow(); dr["WORKDAY"] = workdays[i].WORKDAY; dr["WORKSN"] = workdays[i].WORKSN; dr["PLANID"] = workdays[i].PLANID; dt.Rows.Add(dr); } GuardTourWorkDaysDAL.BulkBatchInsert(dt); }
/******************************** GuardTourWorkDaysDAL**************************************************
public void BulkBatchInsert(System.Data.DataTable dt)
{
SqlBulkCopy bulkCopy = new SqlBulkCopy(this.Context.Data.ConnectionString);
bulkCopy.DestinationTableName = "GuardTour_WorkDays";
bulkCopy.BatchSize = dt.Rows.Count;
bulkCopy.WriteToServer(dt);
}
以上是关于sqlserver2008 批量插入数据的主要内容,如果未能解决你的问题,请参考以下文章
尝试将数据表批量插入 SQL Server 时出现 InvalidOperationException