国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 新巴尔虎右旗| 原平市| 乡城县| 黄石市| 龙州县| 库尔勒市| 金沙县| 九龙县| 大安市| 陕西省| 尚志市| 乐业县| 曲靖市| 志丹县| 夏河县| 旬阳县| 越西县| 东方市| 济阳县| 望奎县| 亚东县| 福安市| 布尔津县| 逊克县| 瑞丽市| 曲靖市| 富平县| 正宁县| 新巴尔虎左旗| 漠河县| 永泰县| 英吉沙县| 德令哈市| 阿鲁科尔沁旗| 蓬溪县| 甘南县| 孟津县| 金昌市| 镇巴县| 普宁市| 花莲市|