SqlBulkCopy插入1亿条数据需要20分钟?
博客专区 > 黄裳23 的博客 > 博客详情
SqlBulkCopy插入1亿条数据需要20分钟?
黄裳23 发表于1年前
SqlBulkCopy插入1亿条数据需要20分钟?
  • 发表于 1年前
  • 阅读 30
  • 收藏 1
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 闲来无事,写了个测试,看一下SqlBulkCopy的优势
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace SQLTest
{
    public class ClassData
    {
        public int GradeID = 0;
        public int ClassID = 0;
        public int StudentID = 0;
        public DateTime Datetime = DateTime.UtcNow;
        public double Data = 0;
        public int GradeIDField
        {
            get { return GradeID; }
            set { GradeID = value; }
        }
        public int ClassIDField
        {
            get { return ClassID; }
            set { ClassID = value; }
        }
        public int StudentIDField
        {
            get { return StudentID; }
            set { StudentID = value; }
        }
        public DateTime DatetimeField
        {
            get { return Datetime; }
            set { Datetime = value; }
        }
        public double DataField
        {
            get { return Data; }
            set { Data = value; }
        }

        public ClassData(int gradeID, int classID, int studentID, DateTime datetime, double data)
        {
            this.GradeID = gradeID;
            this.ClassID = classID;
            this.StudentID = studentID;
            this.Datetime = datetime;
            this.Data = data;
        }
    }
    class Program
    {
        public static void SqlBulkCopyByDatatable(string connectionString, string TableName, DataTable dataTable)
        {

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction))
                {
                    try
                    {
                        sqlbulkcopy.DestinationTableName = TableName;
                        for (int i = 0; i < dataTable.Columns.Count; i++)
                        {
                            sqlbulkcopy.ColumnMappings.Add(dataTable.Columns[i].ColumnName, dataTable.Columns[i].ColumnName);
                        }
                        sqlbulkcopy.WriteToServer(dataTable);
                    }
                    catch (System.Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }

        private static string connectionString = @"Data Source=(local)\myschool;Database=mj_mydatabase;Integrated Security=true";

        static void Main(string[] args)
        {
            DateTime startDateTime = new DateTime(2016, 1, 1, 0, 0, 37);
            DateTime endDateTime = new DateTime(2016, 2, 10, 0, 10, 0);

            SqlConnection myConnection = new SqlConnection(connectionString);
            myConnection.Open();

            while (startDateTime <= endDateTime)
            {
                List<ClassData> dataList = new List<ClassData>();
                int start = 250;
                int step = 1;
                while (step <= 250)
                {
                    int temp = start + step;
                    for (int j = 36; j <= 39; j++)
                    {
                        DateTime realTime = startDateTime;
                        for (int k = 0; k < 5; k++)
                        {
                            dataList.Add(new ClassData(1, temp, j, realTime, 0.333333));
                            realTime = realTime.AddMinutes(1);
                        }
                    }
                    temp = start - step;
                    for (int j = 36; j <= 39; j++)
                    {
                        DateTime realTime = startDateTime;
                        for (int k = 0; k < 5; k++)
                        {
                            dataList.Add(new ClassData(1, temp, j, realTime, 0.333333));
                            realTime = realTime.AddMinutes(1);
                        }
                    }
                    step++;
                }
                startDateTime = startDateTime.AddMinutes(5);
                try
                {
                    DataTable dataTable = new DataTable("MJ_ClassData");
                    dataTable.Columns.Add("GradeID ", Type.GetType("System.Int32"));
                    dataTable.Columns.Add("ClassID", Type.GetType("System.Int32"));
                    dataTable.Columns.Add("StudentID", Type.GetType("System.Int32"));
                    dataTable.Columns.Add("Datetime", Type.GetType("System.DateTime"));
                    dataTable.Columns.Add("Data", Type.GetType("System.Single"));
                    foreach (ClassData data in dataList)
                    {
                        dataTable.Rows.Add(new Object[] { data.GradeID, data.ClassID, data.StudentID, data.Datetime, data.Data });
                    }
                    SqlBulkCopyByDatatable(connectionString, "MJ_ClassData", dataTable);
                }
                finally
                {
                    myConnection.Close();
                }
            }

        }
    }
}


比较搞笑的是,一开始是这样做的,让list的数据全部做完,调用一次sqlbulkcopy,结果...

共有 人打赏支持
粉丝 3
博文 48
码字总数 9420
×
黄裳23
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: