今天想着如何大批量插入数据到数据库中 最开始的想法是使用insert循环插入 但每次都要创建数据库连接 这是非常消耗时间和消耗资源的事。后在网上看到了说使用SqlBulkCopy ,就看了看文档和别人的列子 感觉理解到了一点大概的使用方法 就打算记录下来 方便自己以后使用
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 测试
{
class Program
{
static void Main(string[] args)
{
//创建一个DataTable对象
DataTable dt = new DataTable();
dt.Columns.Add("name",typeof(string));
//给dt内存表插入数据
for (int i = 0; i < 1000000; i++)
{
dt.Rows.Add("测试" + i);
}
System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
st.Start();
using (Sql