SQLServer 數據導入導出的幾種辦法小結。本站提示廣大學習愛好者:(SQLServer 數據導入導出的幾種辦法小結)文章只能為提供參考,不一定能成為您想要的結果。以下是SQLServer 數據導入導出的幾種辦法小結正文
1. SQL Server導入導出領導,這類方法是最便利的.
導入領導,微軟供給了多種數據源驅動,包含SQL Server Native Cliant, OLE DB For Oracle,Flat File Source,Access,Excel,XML等,根本上可以知足體系開辟的需求.
異樣導出領導也有異樣多的目標源驅動,可以把數據導入到分歧的目標源.
對數據庫治理人員來講這類方法簡略輕易操作,導入時SQL Server也會幫你樹立雷同構造的Table.
2. 用.NET的代碼完成(好比有一個txt或是excel的檔案,到讀取到DB中)
2.1 最為罕見的就是輪回讀取txt的內容,然後一條一條的塞入到Table中.這裡不再贅述.
2.2 聚集全體讀取,應用OLEDB驅動.
代碼以下:
string strOLEDBConnect = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\1\;Extended Properties='text;HDR=Yes;FMT=Delimited'";
OleDbConnection conn = new OleDbConnection(strOLEDBConnect);
conn.Open();
SQLstmt = "select * from 1.txt";//讀取.txt中的數據
DataTable dt=new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(SQLstmt, conn);
da.Fill(dt);//在DataSet的指定規模中添加或刷新行以婚配應用DataSet、DataTable 和IDataReader 稱號的數據源中的行。
if(dt.Rows.Count>0)
foreach(DataRow dr in dt.Rows)
{
SQLstmt = "insert into MyTable values('" + dr..."
3.BCP,可以用作年夜容量的數據導入導出,也能夠合營xp_cmdshell來應用.
語法:
BCP語法
bcp {[[database_name.][schema].]{table_name | view_name} | "query"}
{in | out | queryout | format} data_file
[-mmax_errors] [-fformat_file] [-x] [-eerr_file]
[-Ffirst_row] [-Llast_row] [-bbatch_size]
[-ddatabase_name] [-n] [-c] [-N] [-w] [-V (70 | 80 | 90 )]
[-q] [-C { ACP | OEM | RAW | code_page } ] [-tfield_term]
[-rrow_term] [-iinput_file] [-ooutput_file] [-apacket_size]
[-S [server_name[\instance_name]]] [-Ulogin_id] [-Ppassword]
[-T] [-v] [-R] [-k] [-E] [-h"hint [,...n]"]
請留意數據導入導出的偏向參數:in,out,queryout
如:
如:
4.BULK INSERT. T-SQL的敕令,許可直接導入數據
語法:
BULK INSERT
[ database_name. [ schema_name ] . | schema_name. ] [ table_name | view_name ]
FROM 'data_file'
[ WITH
(
[ [ , ] BATCHSIZE =batch_size ]
[ [ , ] CHECK_CONSTRAINTS ]
[ [ , ] CODEPAGE = { 'ACP' | 'OEM' | 'RAW' | 'code_page' } ]
[ [ , ] DATAFILETYPE =
{ 'char' | 'native'| 'widechar' | 'widenative' } ]
[ [ , ] FIELDTERMINATOR = 'field_terminator' ]
[ [ , ] FIRSTROW = first_row ]
[ [ , ] FIRE_TRIGGERS ]
[ [ , ] FORMATFILE ='format_file_path' ]
[ [ , ] KEEPIDENTITY ]
[ [ , ] KEEPNULLS ]
[ [ , ] KILOBYTES_PER_BATCH =kilobytes_per_batch ]
[ [ , ] LASTROW =last_row ]
[ [ , ] MAXERRORS =max_errors ]
[ [ , ] ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) ]
[ [ , ] ROWS_PER_BATCH =rows_per_batch ]
[ [ , ] ROWTERMINATOR ='row_terminator' ]
[ [ , ] TABLOCK ]
[ [ , ] ERRORFILE ='file_name' ]
)]
主要參數:
FIELDTERMINATOR,字段分隔符
FIRSTROW:第一個數據行
ROWTERMINATOR:行終結符
如:
BULK INSERT dbo.ImportTest
FROM 'C:\ImportData.txt'
WITH ( FIELDTERMINATOR =',', FIRSTROW = 2 )
5. OPENROWSET也是T-SQL的敕令,包括有DB銜接的信息和其它導入辦法分歧的是,OPENROWSET可以作為一個目的表介入INSERT,UPDATE,DELETE操作.
語法:
OPENROWSET
( { 'provider_name', { 'datasource';'user_id';'password'
| 'provider_string' }
, { [ catalog. ] [ schema. ] object
| 'query'
}
| BULK 'data_file',
{ FORMATFILE ='format_file_path' [ <bulk_options> ]
| SINGLE_BLOB | SINGLE_CLOB | SINGLE_NCLOB }
} )<bulk_options> ::=
[ , CODEPAGE = { 'ACP' | 'OEM' | 'RAW' | 'code_page' } ]
[ , ERRORFILE ='file_name' ]
[ , FIRSTROW = first_row ]
[ , LASTROW = last_row ]
[ , MAXERRORS = maximum_errors ]
[ , ROWS_PER_BATCH =rows_per_batch ]
如:
INSERT INTO dbo.ImportTest
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\ImportData.xls', 'SELECT * FROM [Sheet1$]' WHERE A1 IS NOT NULL)
6.OPENDATASOURCE
語法:
OPENDATASOURCE ( provider_name,init_string )
如:
INSERT INTO dbo.ImportTest
SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\ImportData.xls;Extended Properties=Excel 8.0')...[Sheet1$]
7.OPENQUERY.是在linked server的基本上履行的查詢.所以履行之前必需先樹立好link server.OPENQUERY的成果集可以作為一個table介入DML的操作.
語法:
OPENQUERY (linked_server ,'query')
如:
EXEC sp_addlinkedserver 'ImportData',
'Jet 4.0', 'Microsoft.Jet.OLEDB.4.0',
'C:\ImportData.xls',
NULL,
'Excel 8.0'
GO
INSERT INTO dbo.ImportTest
SELECT *
FROM OPENQUERY(ImportData, 'SELECT * FROM [Sheet1$]')
以上只是簡略總結的一些DB數據導入導出的辦法及其一些簡略的實例,願望對你理論中會有所贊助.