惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
IT之家
IT之家
T
The Blog of Author Tim Ferriss
V
V2EX
博客园 - 聂微东
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
A
About on SuperTechFans
U
Unit 42
Vercel News
Vercel News
L
LangChain Blog
博客园 - 司徒正美
H
Help Net Security
Recent Announcements
Recent Announcements
Recorded Future
Recorded Future
V
Visual Studio Blog
Jina AI
Jina AI
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
Y
Y Combinator Blog
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
B
Blog RSS Feed
F
Fortinet All Blogs
B
Blog
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
有赞技术团队
有赞技术团队
罗磊的独立博客
博客园_首页
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
量子位
I
InfoQ
小众软件
小众软件
P
Proofpoint News Feed

博客园 - 站在天空下的猪

根据文本框联动下拉框(jquery 插件) - 站在天空下的猪 - 博客园 关于用FOMR提交编码的问题 jquery 图片预览插件 - 站在天空下的猪 - 博客园 自己写的一个jquery模板引擎(json比较好用) Document 对象的常用方法 今天发现梅花雨日历控件蛮好用的哟,腾讯都在用 身份证对应县及县的行政区划代码 【SQLSERVER】存储过程基础 终于解决了一个AjaxPro无刷新的问题了,可以引用在短信方面滴asp.net 2.0 昨天很失败 一个简单的FileUpload处理逻辑 C# 判断是否为数字 DataBinder.Eval总结 asp.net 2.0中实现防盗链 如何把HTML语句直接保存到数据库 如何取得IP/用户名等信息 “ConnectionString 属性尚未初始化”的另类解决办法 asp.net常用函数表 C#中计算两个时间的差
SqlBulkCopy批量转移数据备注
站在天空下的猪 · 2010-05-17 · via 博客园 - 站在天空下的猪

今天测试了SqlBulkCopy,速度确实很快,备份备注记一下:

datatable中每个字段都要和数据表中进行对应,且不能缺少,自增的随便传递数据,sql那边会自己处理

代码

       string moveConn = @"***";
       
string connectionString = @"***";  
     
public string move()
       {
           Stopwatch stopwatch 
= new Stopwatch();
           stopwatch.Start();
           
string result = string.Empty;
           
int count = 0;
           
using (var SqlDB = new Voosay.DataBase.SqlDataBase(moveConn))
           {
               DataTable dt 
= new DataTable();
               dt.TableName 
= "test";
               dt.Columns.Add(
new DataColumn("PkID"typeof(Int64)));
               dt.Columns.Add(
new DataColumn("CustID"typeof(int)));
               dt.Columns.Add(
new DataColumn("PayComID"typeof(long)));
               dt.Columns.Add(
new DataColumn("TypeID"typeof(int)));
               dt.Columns.Add(
new DataColumn("IsEnable"typeof(int)));
               dt.Columns.Add(
new DataColumn("CityID"typeof(int)));
               dt.Columns.Add(
new DataColumn("ClassID"typeof(int)));
               dt.Columns.Add(
new DataColumn("CreateTime"typeof(DateTime)));
               
string sql = "***;
               DataSet ds = SqlDB.Datasql(sql);
               
if (ds.Tables[0].Rows.Count > 0)
               {
                   
int i = 1;
                   
foreach (DataRow row in ds.Tables[0].Rows)
                   {
                       DataRow newrows 
= dt.NewRow();
                       
int t = Convert.ToInt32(row["PaymentPlatform"]);
                       newrows[
"PkID"= 1;
                       newrows[
"CustID"= 20100520;
                       newrows[
"PayComID"= Convert.ToInt64(row["appkey"]);
                       newrows[
"TypeID"= t;
                       newrows[
"IsEnable"= Convert.ToInt32(row["Enable"]);
                       newrows[
"CityID"= Convert.ToInt32(row["SiteID"]);
                       newrows[
"ClassID"= 1;
                       
if (t < 10)
                       {
                           newrows[
"ClassID"= 0;
                       }
                       newrows[
"CreateTime"= Convert.ToDateTime(row["Createdate"]);
                       dt.Rows.Add(newrows);
                       Console.WriteLine(
string.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}",newrows["CustID"],newrows["PayComID"],newrows["TypeID"]
                           ,newrows[
"IsEnable"],newrows["CityID"],newrows["ClassID"],newrows["CreateTime"]));
                       i
++;
                   }
                   movedata(dt);
                   count 
= ds.Tables[0].Rows.Count;
               }

           }
           stopwatch.Stop();
           result 

= string.Format("执行{0}条记录转移,运行时间为:{1}毫秒", count, stopwatch.ElapsedMilliseconds);
           
return result;
       }
       
private void movedata(DataTable dt)
       {
           SqlBulkCopy sqlBulkCopy 
= new SqlBulkCopy(connectionString);
           sqlBulkCopy.DestinationTableName 
= "*";
           sqlBulkCopy.BatchSize 
= dt.Rows.Count;
           SqlConnection sqlConnection 
= new SqlConnection(connectionString);
           sqlConnection.Open();
           
if (dt != null && dt.Rows.Count != 0)
           {
               sqlBulkCopy.WriteToServer(dt);
           }
           sqlBulkCopy.Close();
           sqlConnection.Close();
       }