asp.net 使用SqlBulkCopy極速插入數(shù)據(jù)到 SQL Server
于是乎,下demo,測(cè)試,改成自己一般使用的方法測(cè)試,NND,還真可以說是極速。
在此貼上我的Demo:SqlBulkCopy.rar
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ì)比
- 使用SqlBulkCopy時(shí)應(yīng)注意Sqlserver表中使用缺省值的列
- C#中使用ADOMD.NET查詢多維數(shù)據(jù)集的實(shí)現(xiàn)方法
- C#數(shù)據(jù)庫(kù)操作類AccessHelper實(shí)例
- C#利用Openxml讀取Excel數(shù)據(jù)實(shí)例
- C#百萬數(shù)據(jù)查詢出現(xiàn)超時(shí)問題的解決方法
- C#使用DataSet Datatable更新數(shù)據(jù)庫(kù)的三種實(shí)現(xiàn)方法
- C#應(yīng)用BindingSource實(shí)現(xiàn)數(shù)據(jù)同步的方法
- C#將Sql數(shù)據(jù)保存到Excel文件中的方法
- C#中遍歷DataSet數(shù)據(jù)集對(duì)象實(shí)例
- C#使用晚綁定來實(shí)現(xiàn)壓縮Access數(shù)據(jù)庫(kù)的方法
- C# Oracle數(shù)據(jù)庫(kù)操作類實(shí)例詳解
- C#使用SqlBulkCopy批量復(fù)制數(shù)據(jù)到數(shù)據(jù)表
相關(guān)文章
SQL Server中關(guān)于基數(shù)估計(jì)計(jì)算預(yù)估行數(shù)的一些方法探討
這篇文章主要介紹了SQL Server中關(guān)于基數(shù)估計(jì)計(jì)算預(yù)估行數(shù)的一些方法探討,需要的朋友可以參考下2017-05-05SQL Server誤區(qū)30日談 第29天 有關(guān)堆碎片的誤區(qū)
對(duì)堆建聚集索引再DROP在我看來是除了收縮數(shù)據(jù)庫(kù)之外最2的事了2013-01-01SQLserver2000 企業(yè)版 出現(xiàn)"進(jìn)程51發(fā)生了嚴(yán)重的異常"錯(cuò)誤的處理方法
SQL2000 企業(yè)版 出現(xiàn)“進(jìn)程51發(fā)生了嚴(yán)重的異?!卞e(cuò)誤的解決方法,利用了微軟官方的工具。2009-07-07SQL小技巧 又快又簡(jiǎn)單的得到你的數(shù)據(jù)庫(kù)每個(gè)表的記錄數(shù)
說到如何得到表的行數(shù),大家首先想到的應(yīng)該是select count(*) from table1....2009-09-09SQL Server誤區(qū)30日談 第18天 有關(guān)FileStream的存儲(chǔ),垃圾回收以及其它
由于FileStream數(shù)據(jù)容器(指的是存放FileStream文件的NTFS文件夾,杜撰出來的術(shù)語(yǔ))必須像數(shù)據(jù)文件或日志文件那樣符合本地存儲(chǔ)策略-也就是說,這個(gè)數(shù)據(jù)容器必須放在對(duì)于運(yùn)行SQL Server的Windows Server是本地存儲(chǔ)2013-01-01SQL Server誤區(qū)30日談 第23天 有關(guān)鎖升級(jí)的誤區(qū)
在SQL Server 2005和之前的版本,頁(yè)鎖會(huì)直接升級(jí)到表鎖。在SQL Server 2005或SQL Server 2008,你可以通過如下跟蹤標(biāo)志改變鎖升級(jí)的行為2013-01-01SQL Server中的SELECT會(huì)阻塞SELECT嗎
這篇文章主要給大家介紹了SQL Server中的SELECT會(huì)阻塞SELECT的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12MSSQL SERVER 2005 數(shù)學(xué)函數(shù)整理
MSSQL SERVER 2005 數(shù)學(xué)函數(shù)整理2010-06-06