国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > SQL Server > 正文

asp.net 使用SqlBulkCopy極速插入數(shù)據(jù)到 SQL Server

2024-08-31 00:58:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
按這個(gè)來(lái)算,我們那個(gè)發(fā)水票的時(shí)間就會(huì)由 10分鐘-->20秒,這可太神奇了。
于是乎,下demo,測(cè)試,改成自己一般使用的方法測(cè)試,NND,還真可以說(shuō)是極速。
在此貼上我的Demo:SqlBulkCopy.rar

復(fù)制代碼 代碼如下:


using System;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
namespace ConsoleAppInsertTest
{
class Program
{
static int count = 1000000; //插入的條數(shù)
static void Main(string[] args)
{
long sqlBulkCopyInsertRunTime = SqlBulkCopyInsert();
Console.WriteLine(string.Format("使用SqlBulkCopy插入{1}條數(shù)據(jù)所用的時(shí)間是{0}毫秒", sqlBulkCopyInsertRunTime, count));
long commonInsertRunTime = CommonInsert();
Console.WriteLine(string.Format("普通方式插入{1}條數(shù)據(jù)所用的時(shí)間是{0}毫秒", commonInsertRunTime, count));
Console.ReadKey();
}
/// <summary>
/// 使用普通插入數(shù)據(jù)
/// </summary>
/// <returns></returns>
private static long CommonInsert()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < count; i++)
{
SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnection, CommandType.Text, "insert into passport(PassportKey) values('" + Guid.NewGuid() + "')");
}
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds;
}
/// <summary>
/// 使用SqlBulkCopy方式插入數(shù)據(jù)
/// </summary>
/// <returns></returns>
private static long SqlBulkCopyInsert()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
DataTable dataTable = GetTableSchema();
for (int i = 0; i < count; i++)
{
DataRow dataRow = dataTable.NewRow();
dataRow[2] = Guid.NewGuid();
dataTable.Rows.Add(dataRow);
}
//Console.WriteLine(stopwatch.ElapsedMilliseconds);//初始化數(shù)據(jù)時(shí)間
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(SqlHelper.SqlConnection);
sqlBulkCopy.DestinationTableName = "Passport";
if (dataTable != null && dataTable.Rows.Count != 0)
{
sqlBulkCopy.WriteToServer(dataTable);
}
sqlBulkCopy.Close();
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds;
}
private static DataTable GetTableSchema()
{
return SqlHelper.ExecuteDataset(SqlHelper.SqlConnection, CommandType.Text, "select * from Passport where 1=2").Tables[0];
}
}
}


轉(zhuǎn)自cnblogs的文章 SQL批量插入數(shù)據(jù)幾種方案的性能詳細(xì)對(duì)比
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 甘肃省| 巢湖市| 磐石市| 额尔古纳市| 麻江县| 上林县| 正宁县| 雅安市| 安国市| 宣恩县| 云林县| 儋州市| 德昌县| 开江县| 金川县| 嘉善县| 仁化县| 宁乡县| 深水埗区| 若尔盖县| 龙泉市| 雅安市| 洪洞县| 曲水县| 敦煌市| 镶黄旗| 大方县| 革吉县| 普兰店市| 云安县| 长丰县| 霸州市| 奉贤区| 淮北市| 吴江市| 庄河市| 吉首市| 民和| 民权县| 酒泉市| 兴业县|