zoukankan      html  css  js  c++  java
  • 使用SqlBulkCopy批量插入数据,测试20万条用时5秒

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.Data;
    using System.Data.SqlClient;

    namespace confirmBeiZhu
    {
    class Program
    {
    static void Main(string[] args)
    {
    fabu();
    }

    protected static void fabu()
    {
    DateTime time1 = DateTime.Now;
    string connectionString = "Max Pool Size=100000; Data Source=localhost;Initial Catalog=collection40;User ID=sa;Password=ccc";
    SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction);
    sqlbulkcopy.DestinationTableName = "xyAddress";//数据库中的表名

    SqlConnection sqlconnection = new SqlConnection(connectionString);
    SqlDataAdapter sqldataadapter = new SqlDataAdapter("select * from xyAddress where 1=2", sqlconnection);
    DataSet dataset = new DataSet();
    sqldataadapter.Fill(dataset, "xyAddress");
    DataTable datatable = dataset.Tables[0];
    //生成20000条记录

    //INSERT INTO [XyAddress] ([], [], [], [], [], [], [], [], [], []) VALUES ('', '', '', '', '', '', '', '', '', '')
    for (int i = 0; i < 2; i++)
    {
    DataRow dr = datatable.NewRow();
    dr["relation"] = "本人";
    dr["xm"] = "张三" + i.ToString();
    dr["leixing"] = "你好类型";
    dr["dizhi"] = "你好2123" + i.ToString();
    dr["hide"] = "0";
    datatable.Rows.Add(dr);
    }

    sqlbulkcopy.WriteToServer(dataset.Tables[0]);

    if (null != sqlconnection)
    {
    if (sqlconnection.State == ConnectionState.Open)
    {
    sqlconnection.Close();
    sqlconnection.Dispose();
    }
    sqlconnection.Dispose();
    }

    DateTime time2 = DateTime.Now;
    TimeSpan ts = time2 - time1;

    Console.WriteLine("已处理完毕" + ts.TotalSeconds.ToString());

    Console.ReadLine();
    }


    }
    }

  • 相关阅读:
    写在毕业散伙饭后
    关于2005年高考志愿填报注意事项与分析
    如何在ASP.NET页面间传送数据
    MS Server 2000中的Table类型
    离职申请[转]
    JQUERY 获取text,areatext,radio,checkbox,select值
    DropdownList用法记录
    SQL小记
    认识SortedList类
    js中的|| 与 &&
  • 原文地址:https://www.cnblogs.com/yubufan/p/6958098.html
Copyright © 2011-2022 走看看