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);
}

  

posted @ 2019-01-10 09:20  沈阳晓东  阅读(655)  评论(0编辑  收藏  举报