zoukankan      html  css  js  c++  java
  • C# 批量插入数据方法

    批量插入数据方法

    void InsertTwo(List<CourseArrangeInfo> dtF)
    {
    Stopwatch watch = new Stopwatch();
    watch.Start();
    DataTable dt = new DataTable();//准备存放数据的临时表
    //构造表的列
    dt.Columns.Add("ShoppingCartID");
    dt.Columns.Add("Produce_ID");
    dt.Columns.Add("Number");
    dt.Columns.Add("Customer_ID");
    dt.Columns.Add("CreateTime");
    foreach (var item in dtF)
    {
    DataRow rows = dt.NewRow();
    //rows["ShoppingCartID"] = System.Guid.NewGuid().ToString();
    //rows["Produce_ID"] = item.Produce_ID;
    //rows["Number"] = item.Number;
    //rows["Customer_ID"] = item.Customer_ID;
    //rows["CreateTime"] = DateTime.Now;
    //dt.Rows.Add(rows);
    }
    //重点代码处理方式
    var ss = ConfigurationManager.ConnectionStrings["default"].ConnectionString;
    string connStr = CEncrypt.DecryString(ss);//连接字符串
    using (SqlBulkCopy sqlBulkcopy = new SqlBulkCopy(connStr))
    {
    sqlBulkcopy.DestinationTableName = "ShoppingCart";//定义需要存入数据库中的相应的表的名称
    sqlBulkcopy.ColumnMappings.Add("ShoppingCartID", "ShoppingCartID");
    sqlBulkcopy.ColumnMappings.Add("Produce_ID", "Produce_ID");
    sqlBulkcopy.ColumnMappings.Add("Number", "Number");
    sqlBulkcopy.ColumnMappings.Add("Customer_ID", "Customer_ID");
    sqlBulkcopy.ColumnMappings.Add("CreateTime", "CreateTime");
    sqlBulkcopy.WriteToServer(dt);
    }
    watch.Stop();
    }

  • 相关阅读:
    C++类模板的三种特化
    构造函数抛异常
    ScrapySharp
    QtWebKit
    visual studio 插件
    asp.net 开源
    cfr
    eclipse icon
    JAD Java Decompiler
    Java Decompiler
  • 原文地址:https://www.cnblogs.com/zhy-1992/p/7929525.html
Copyright © 2011-2022 走看看