SqlBulkCopy类数据导入(自己测试例子)

SqlBulkCopy类数据导入(自己测试例子),第1张

概述 public static readonly string ConnData = "Data Source=192.168.0.100;Initial Catalog=DBName;User ID=sa;Password=123456";         protected void Page_Load(object sender, EventArgs e)         {         

 public static Readonly string ConnData = "Data Source=192.168.0.100;Initial Catalog=DBname;User ID=sa;Password=123456";

        protected voID Page_Load(object sender,EventArgs e)
        {
            if (!IsPostBack)
            {
                Datatable dt = new Datatable();
                dt.Columns.Add("ScheduleKey",typeof(GuID));
                dt.Columns.Add("adminUserKey",typeof(GuID));
                dt.Columns.Add("ScheduleRuleKey",typeof(GuID));
                dt.Columns.Add("eBayProfileKey",typeof(GuID));
                dt.Columns.Add("LaunchTimeSite",typeof(DateTime));
                dt.Columns.Add("CreationDate",typeof(DateTime));
                for (int i = 0; i < 7000; i++)
                {
                    DaTarow dr = dt.NewRow();
                    dr["ScheduleKey"] = GuID.NewGuID();
                    dr["adminUserKey"] = GuID.NewGuID();
                    dr["ScheduleRuleKey"] = GuID.NewGuID();
                    dr["eBayProfileKey"] = GuID.NewGuID();
                    dr["LaunchTimeSite"] = DateTime.Now;
                    dr["CreationDate"] = DateTime.Now;
                    dt.Rows.Add(dr);
                }

                sqlBulkcopy(dt);
            }
        }

        #region   批量插入数据到数据库
        DateTime startTime;
        private bool sqlBulkcopy(Datatable dt)
        {

            try
            {
                startTime = DateTime.Now;
                //数据批量导入sqlserver,创建实例     sqlBulkcopyOptions.UseInternalTransaction采用事务   复制失败自动回滚
                System.Data.sqlClIEnt.sqlBulkcopy sqlbulk = new System.Data.sqlClIEnt.sqlBulkcopy(ConnData,sqlBulkcopyOptions.UseInternalTransaction);
                sqlbulk.sqlRowscopIEd +=
                new sqlRowscopIEdEventHandler(OnRowscopIEd); //订阅复制完成后的方法,参数是 sqlbulk.NotifyAfter的值
                sqlbulk.NotifyAfter = dt.Rows.Count;

                //目标数据库表名
                sqlbulk.Destinationtablename = "pa_ScheduleShadow";
                //数据集字段索引与数据库字段索引映射
                sqlbulk.columnmappings.Add(0,"ScheduleKey");
                sqlbulk.columnmappings.Add(1,"adminUserKey");
                sqlbulk.columnmappings.Add(2,"ScheduleRuleKey");
                sqlbulk.columnmappings.Add(3,"eBayProfileKey");
                sqlbulk.columnmappings.Add(4,"LaunchTimeSite");
                sqlbulk.columnmappings.Add(5,"CreationDate");
                //导入
                sqlbulk.WritetoServer(dt);
                sqlbulk.Close();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dt.dispose();
            }
        }

        //复制完成后的处理事件        private voID OnRowscopIEd(object sender,sqlRowscopIEdEventArgs args)        {            lblCounter.Text += args.RowscopIEd.ToString() + " 条记录已导入";            TimeSpan copyTime = DateTime.Now - startTime;            lblCounter.Text += "&nbsp;&nbsp;花费时间:" + copyTime.Seconds.ToString() + "." +            copyTime.Milliseconds.ToString() + " 秒";        }        #endregion       

总结

以上是内存溢出为你收集整理的SqlBulkCopy类数据导入(自己测试例子)全部内容,希望文章能够帮你解决SqlBulkCopy类数据导入(自己测试例子)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1181486.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存