1. SQL Server導(dǎo)入導(dǎo)出向?qū)?這種方式是最方便的.
導(dǎo)入向?qū)?微軟提供了多種數(shù)據(jù)源驅(qū)動(dòng),包括SQL Server Native Cliant, OLE DB For Oracle,Flat File Source,Access,Excel,XML等,基本上可以滿足系統(tǒng)開發(fā)的需求.
同樣導(dǎo)出向?qū)б灿型瑯佣嗟哪康脑打?qū)動(dòng),可以把數(shù)據(jù)導(dǎo)入到不同的目的源.
對(duì)數(shù)據(jù)庫管理人員來說這種方式簡單容易操作,導(dǎo)入時(shí)SQL Server也會(huì)幫你建立相同結(jié)構(gòu)的Table.
2. 用.NET的代碼實(shí)現(xiàn)(比如有一個(gè)txt或是excel的檔案,到讀取到DB中)
2.1 最為常見的就是循環(huán)讀取txt的內(nèi)容,然后一條一條的塞入到Table中.這里不再贅述.
2.2 集合整體讀取,使用OLEDB驅(qū)動(dòng).
代碼如下:
復(fù)制代碼 代碼如下:
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中的數(shù)據(jù)
DataTable dt=new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(SQLstmt, conn);
da.Fill(dt);//在DataSet的指定范圍中添加或刷新行以匹配使用DataSet、DataTable 和IDataReader 名稱的數(shù)據(jù)源中的行。
if(dt.Rows.Count>0)
foreach(DataRow dr in dt.Rows)
{
SQLstmt = "insert into MyTable values('" + dr..."
3.BCP,可以用作大容量的數(shù)據(jù)導(dǎo)入導(dǎo)出,也可以配合xp_cmdshell來使用.
語法:
BCP語法
復(fù)制代碼 代碼如下:
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]"]
請(qǐng)注意數(shù)據(jù)導(dǎo)入導(dǎo)出的方向參數(shù):in,out,queryout
如:
如:
4.BULK INSERT. T-SQL的命令,允許直接導(dǎo)入數(shù)據(jù)
語法:
復(fù)制代碼 代碼如下:
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' ]
)]
重要參數(shù):
FIELDTERMINATOR,字段分隔符
FIRSTROW:第一個(gè)數(shù)據(jù)行
ROWTERMINATOR:行終結(jié)符
如:
復(fù)制代碼 代碼如下:
BULK INSERT dbo.ImportTest
FROM 'C:\ImportData.txt'
WITH ( FIELDTERMINATOR =',', FIRSTROW = 2 )
5. OPENROWSET也是T-SQL的命令,包含有DB連接的信息和其它導(dǎo)入方法不同的是,OPENROWSET可以作為一個(gè)目標(biāo)表參與INSERT,UPDATE,DELETE操作.
語法:
復(fù)制代碼 代碼如下:
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 ]
如:
復(fù)制代碼 代碼如下:
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 )
如:
復(fù)制代碼 代碼如下:
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的基礎(chǔ)上執(zhí)行的查詢.所以執(zhí)行之前必須先建立好link server.OPENQUERY的結(jié)果集可以作為一個(gè)table參與DML的操作.
語法:
OPENQUERY (linked_server ,'query')
如:
復(fù)制代碼 代碼如下:
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$]')
以上只是簡單總結(jié)的一些DB數(shù)據(jù)導(dǎo)入導(dǎo)出的方法及其一些簡單的實(shí)例,希望對(duì)你實(shí)踐中會(huì)有所幫助.
您可能感興趣的文章:- 圖文教程mssqlserver數(shù)據(jù)庫導(dǎo)出到另外一個(gè)數(shù)據(jù)庫的方法
- SQLServer導(dǎo)出數(shù)據(jù)到MySQL實(shí)例介紹
- SQLServer導(dǎo)出sql文件/表架構(gòu)和數(shù)據(jù)操作步驟
- sqlserver bcp(數(shù)據(jù)導(dǎo)入導(dǎo)出工具)一般用法與命令詳解
- sqlserver 導(dǎo)出插入腳本代碼
- SQLserver 2008將數(shù)據(jù)導(dǎo)出到Sql腳本文件的方法
- 使用Navicat Premium將SQLServer數(shù)據(jù)導(dǎo)出為sql格式