SQL Server 批量插入資料的兩種方法

2022-09-08 07:45:13 字數 3924 閱讀 1921

在sql server

中插入一條資料使用insert語句,但是如果想要批量插入一堆資料的話,迴圈使用insert不僅效率低,而且會導致sql一系統效能問題。下面介紹

sql server支援的兩種批量資料插入方法:bulk和錶值引數(table-valued parameters)。

執行下面的指令碼,建立測試資料庫和錶值引數。

view plain

copy to clipboard

print

?--create database  

create database bulktestdb;  

go  

use bulktestdb;  

go  

--create table  

create table bulktesttable(  

id int

primary key,  

username nvarchar(32),  

pwd varchar(16))  

go  

--create table valued  

create type bulkudt as table  

(id int

,  username nvarchar(32),  

pwd varchar(16))  

下面我們使用最簡單的insert語句來插入100萬條資料,**如下:

view plain

copy to clipboard

print

?stopwatch sw = 

newstopwatch();  

sqlconnection sqlconn = new

sqlconnection(  

configurationmanager.connectionstrings["connstr"

].connectionstring);

//連線資料庫

sqlcommand sqlcomm = new

sqlcommand();  

sqlcomm.commandtext = string

.format(

"insert into bulktesttable(id,username,pwd)values(@p0,@p1,@p2)"

);//引數化sql

sqlcomm.parameters.add("@p0"

, sqldbtype.int);  

sqlcomm.parameters.add("@p1"

, sqldbtype.nvarchar);  

sqlcomm.parameters.add("@p2"

, sqldbtype.varchar);  

sqlcomm.commandtype = commandtype.text;  

sqlcomm.connection = sqlconn;  

sqlconn.open();  

try"

, count * multiply);  

sqlcomm.parameters["@p2"

].value = 

string

.format(

"pwd-"

, count * multiply);  

sw.start();  

sqlcomm.executenonquery();  

sw.stop();  

}  //每插入10萬條資料後,顯示此次插入所用時間

console.writeline(string

.format(

"elapsed time is  milliseconds"

, sw.elapsedmilliseconds));  

}  }  

catch

(exception ex)  

finally

console.readline();  

耗時圖如下:

由於執行過慢,才插入10萬條就耗時72390 milliseconds,所以我就手動強行停止了。

下面看一下使用bulk插入的情況:

bulk方法主要思想是通過在客戶端把資料都快取在table中,然後利用sqlbulkcopy一次性把table中的資料插入到資料庫

**如下:

view plain

copy to clipboard

print

?public

static

void

bulktodb(datatable dt)  

catch

(exception ex)  

finally

}  public

static

datatable gettableschema()  

);  

return

dt;  

}  static

void

main(

string

args)  

", count * multiply);  

r[2] = string

.format(

"pwd-"

, count * multiply);  

dt.rows.add(r);  

}  sw.start();  

bulk.bulktodb(dt);  

sw.stop();  

console.writeline(string

.format(

"elapsed time is  milliseconds"

, sw.elapsedmilliseconds));  

}  console.readline();  

}  耗時圖如下:

可見,使用bulk後,效率和效能明顯上公升。使用insert插入10萬資料耗時72390,而現在使用bulk插入100萬資料才耗時17583。

最後再看看使用錶值引數的效率,會另你大為驚訝的。

錶值引數是sql server 2008新特性,簡稱tvps。對於錶值引數不熟悉的朋友,可以參考最新的book online,我也會另外寫一篇關於錶值引數的部落格,不過此次不對錶值引數的概念做過多的介紹。言歸正傳,看**:

view plain

copy to clipboard

print

?public

static

void

tablevaluedtodb(datatable dt)  

}  catch

(exception ex)  

finally

}  public

static

datatable gettableschema()  

);  

return

dt;  

}  static

void

main(

string

args)  

", count * multiply);  

r[2] = string

.format(

"pwd-"

, count * multiply);  

dt.rows.add(r);  

}  sw.start();  

tablevalued.tablevaluedtodb(dt);  

sw.stop();  

console.writeline(string

.format(

"elapsed time is  milliseconds"

, sw.elapsedmilliseconds));  

}  console.readline();  

}  耗時圖如下:

比bulk還快5秒。



SQL Server批量插入資料

有這樣乙個表 operatorinfo 欄位有 operator no operator pwd operator name group id group no skill level operator type pausenum operator acd type 其中 operator no 需...

sql server 批量插入資料demo

測試表結構如下 create table dbo tblphonenum id bigint identity 1,1 not null,phonenum nvarchar 50 not null,name nvarchar 50 not null,source nvarchar 50 null,c...

使用SqlServer資料批量插入

一 sqlserver資料批量插入 sqlserver的批量插入很簡單,使用sqlbulkcopy就可以,以下是該類的實現 為 system.data.sqlclient 提供的用於批量操作的方法。public sealed class mssqlbatcher ibatcherprovider 將...