码迷,mamicode.com
首页 > 数据库 > 详细

sqlserver2008 批量插入数据

时间:2019-01-10 10:55:27      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:highlight   ++   table   datatable   tables   this   div   serve   batch   

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 批量插入数据

标签:highlight   ++   table   datatable   tables   this   div   serve   batch   

原文地址:https://www.cnblogs.com/Y-X-DONG/p/10247998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!