zoukankan      html  css  js  c++  java
  • 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;
            }
    

      

         

  • 相关阅读:
    How to change the property of a control from a flowlayoutpanel?
    Add controls dynamically in flowlayoutpanel
    CLR via C# 读书笔记 6-2 不同AppDomain之间的通信 z
    应用程序域 z
    C# 在SQLite数据库中存储图像 z
    Using .NET 4's Lazy<T> 实现单实例
    tpl demo
    SQLite批量插入,修改数据库 zt
    Dev表格导出工具类 z
    easyui 设置一加载,搜索框立即弹出的效果
  • 原文地址:https://www.cnblogs.com/yuanye0918/p/7521258.html
Copyright © 2011-2022 走看看