SQLSERVER_SqlBulkCopy批量添加使用

直接上代码了

         static void Main(string[] args)
        {
            string conn = @"Initial Catalog=aaa;Data Source = 192.168.1.1; Integrated Security = False; User ID = sa; Password=123;MultipleActiveResultSets=True;Encrypt=False;TrustServerCertificate=False";

  
            SqlBulkCopyByDatatable(conn, "Permission", GetTableSchema());

        }

  

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

  

 sstatic DataTable GetTableSchema()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] {
 new DataColumn("Id",typeof(Guid)),
 new DataColumn("MenuID",typeof(Guid)),
  new DataColumn("MenuFunctionID",typeof(Guid)),
 new DataColumn("RoleID",typeof(Guid)),
  new DataColumn("UserId",typeof(Guid)),
 new DataColumn("PerStr",typeof(Boolean)),
  new DataColumn("ParentId",typeof(Guid)),
 new DataColumn("PlantId",typeof(Guid)),
  new DataColumn("CreateTime",typeof(DateTime)),
 new DataColumn("Remark",typeof(string)),
  new DataColumn("UpdateTime",typeof(DateTime)),
 new DataColumn("DeleteState",typeof(int)),
 new DataColumn("CreateUserId",typeof(Guid)),
  new DataColumn("CreateUserName",typeof(string)),
  new DataColumn("UpdateUserId",typeof(Guid)),
 new DataColumn("UpdateUserName",typeof(string))});

            for (int i = 0; i < 101; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = Guid.NewGuid();
                dr[1] = Guid.NewGuid();
                dr[2] = Guid.NewGuid();
                dr[3] = Guid.NewGuid();
                dr[4] = Guid.NewGuid();
                dr[5] = true;
                dr[6] = Guid.NewGuid();
                dr[7] = Guid.NewGuid();
                dr[8] = DateTime.Now;
                dr[9] = "备注";
                dr[10] = DateTime.Now; ;
                dr[11] = (int)i;
                dr[12] = Guid.NewGuid();
                dr[13] = "测试";
                dr[14] = Guid.NewGuid();
                dr[15] = "测试2";
                dt.Rows.Add(dr);
            }

            return dt;
        }

  //下面的案例是带Stopwatch 测试耗时

        static void Insert()
        {
            Console.WriteLine("跑批开始");
            int tol = 1020000;
            Stopwatch sw = new Stopwatch();
            DataTable dt = GetTableSchemas();
            using (SqlConnection conn = new SqlConnection("Initial Catalog=SchneiderETO;Data Source=192.168.1.205;Integrated Security=False;User ID=eto;Password=123;MultipleActiveResultSets=True;Encrypt=False;TrustServerCertificate=False"))
            {
                try
                {


                    SqlBulkCopy bulkCopy = new SqlBulkCopy(conn);
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandTimeout = 180;
                    bulkCopy.DestinationTableName = "aaaaa";
                    bulkCopy.BatchSize = dt.Rows.Count;
                    conn.Open();
                    sw.Start();
                    for (int i = 0; i < tol; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr[0] = Guid.NewGuid();
                        dr[1] = Guid.NewGuid();
                        dr[2] = Guid.NewGuid();
                        dr[3] = Guid.NewGuid();
                        dr[4] = Guid.NewGuid();
                        dr[5] = true;
                        dr[6] = Guid.NewGuid();
                        dr[7] = Guid.NewGuid();
                        dr[8] = DateTime.Now;
                        dr[9] = "备注";
                        dr[10] = DateTime.Now; ;
                        dr[11] = (int)i;
                        dr[12] = Guid.NewGuid();
                        dr[13] = "测试";
                        dr[14] = Guid.NewGuid();
                        dr[15] = "测试2";
                        dt.Rows.Add(dr);
                    }
                    if (dt != null && dt.Rows.Count != 0)
                    {
                        bulkCopy.WriteToServer(dt);
                        sw.Stop();
                    }
                    conn.Close();
                    Console.WriteLine(string.Format("插入{0}条记录共花费{1}毫秒,{2}分钟", tol, sw.ElapsedMilliseconds, GetMinute(sw.ElapsedMilliseconds)));
                    Console.ReadKey();
                }
                catch (Exception ex)
                {

                    throw new Exception(ex.Message);
                }
            }
        }

  表字段,与上面一样。

        static DataTable GetTableSchemas()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] {
 new DataColumn("Id",typeof(Guid)),
 new DataColumn("MenuID",typeof(Guid)),
  new DataColumn("MenuFunctionID",typeof(Guid)),
 new DataColumn("RoleID",typeof(Guid)),
  new DataColumn("UserId",typeof(Guid)),
 new DataColumn("PerStr",typeof(Boolean)),
  new DataColumn("ParentId",typeof(Guid)),
 new DataColumn("PlantId",typeof(Guid)),
  new DataColumn("CreateTime",typeof(DateTime)),
 new DataColumn("Remark",typeof(string)),
  new DataColumn("UpdateTime",typeof(DateTime)),
 new DataColumn("DeleteState",typeof(int)),
 new DataColumn("CreateUserId",typeof(Guid)),
  new DataColumn("CreateUserName",typeof(string)),
  new DataColumn("UpdateUserId",typeof(Guid)),
 new DataColumn("UpdateUserName",typeof(string))});



            return dt;
        }

  记录时间

        static int GetMinute(long l)
        {
            return (Int32)l / 60000;
        }

  

 

 

     

posted on 2017-09-14 16:15  嘿呀土豆  阅读(358)  评论(0编辑  收藏  举报

导航