SqlBulkCopy 小例子.抄的别个的哈.

///群里面有位朋友发的,对自己还有点子用,先就存下了,关键是sqlbulkcopy吧,这个还真还没有用过.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
        public void CopySQL()
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("LoginName");
            dt.Columns.Add("LoginPWD");

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                DataRow dr = dt.NewRow();

                dr["LoginName"] = "chk" + i;
                dr["LoginPWD"] = i.ToString();

                dt.Rows.Add(dr);
               
            }
            Stopwatch sw = new Stopwatch();
            TimeSpan ts = new TimeSpan();
            sw.Start();

            SqlConnection scon = new SqlConnection();
            scon.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;

            scon.Open();

            SqlBulkCopy sc = new SqlBulkCopy(scon);
            sc.BatchSize = 10000;
            sc.BulkCopyTimeout = 10;
            sc.DestinationTableName = "LoginTable";
            sc.ColumnMappings.Add("LoginName", "LoginName");
            sc.ColumnMappings.Add("LoginPWD", "LoginPWD");
            sc.SqlRowsCopied += new SqlRowsCopiedEventHandler(sc_SqlRowsCopied);
            sc.NotifyAfter = 1000;
            sc.WriteToServer(dt);
            sw.Stop();
            ts = sw.Elapsed;


        }

        void sc_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
        {
            Console.WriteLine(e.RowsCopied.ToString());
        }
    }

}

你可能感兴趣的:(copy)