一、sql server 和access的數(shù)據(jù)導入導出 
常規(guī)的數(shù)據(jù)導入導出:
使用dts向導遷移你的access數(shù)據(jù)到sql server,你可以使用這些步驟:
1在sql server企業(yè)管理器中的tools(工具)菜單上,選擇data transformation
2services(數(shù)據(jù)轉換服務),然后選擇 czdimport data(導入數(shù)據(jù))。
3在choose a data source(選擇數(shù)據(jù)源)對話框中選擇microsoft access as the source,然后鍵入你的.mdb數(shù)據(jù)庫(.mdb文件擴展名)的文件名或通過瀏覽尋找該文件。
4在choose a destination(選擇目標)對話框中,選擇microsoft ole db prov ider for sql server,選擇數(shù)據(jù)庫服務器,然后單擊必要的驗證方式。
5在specify table copy(指定表格復制)或query(查詢)對話框中,單擊copy tables(復制表格)。
6在select source tables(選擇源表格)對話框中,單擊select all(全部選定)。下一步,完成。
transact-sql語句進行導入導出:
1. 在sql server里查詢access數(shù)據(jù):
-- ======================================================
select *
from opendatasource( 'microsoft.jet.oledb.4.0',
'data source="c:/db.mdb";user id=admin;password=')...表名
2.將access導入sql server
-- ======================================================
在sql server 里運行:
select *
into newtable
from opendatasource ('microsoft.jet.oledb.4.0',
'data source="c:/db.mdb";user id=admin;password=' )...表名
3. 將sql server表里的數(shù)據(jù)插入到access表中 
-- ======================================================
在sql server 里運行:
insert into opendatasource( 'microsoft.jet.oledb.4.0',
'data source=" c:/db.mdb";user id=admin;password=')...表名
(列名1,列名2)
select 列名1,列名2 from sql表
實例:
insert into openrowset('microsoft.jet.oledb.4.0',
'c:/db.mdb';'admin';'', test)
select id,name from test
insert into openrowset('microsoft.jet.oledb.4.0', 'c:/trade.mdb'; 'admin'; '', 表名) 
select *
from sqltablename
二、 sql server 和excel的數(shù)據(jù)導入導出 
1、在sql server里查詢excel數(shù)據(jù):
-- ======================================================
select *
from opendatasource( 'microsoft.jet.oledb.4.0',
'data source="c:/book1.xls";user id=admin;password=;extended properties=excel 5.0')...[sheet1$]
下面是個查詢的示例,它通過用于 jet 的 ole db 提供程序查詢 excel 電子表格。
select * 
from opendatasource ( 'microsoft.jet.oledb.4.0', 
 'data source="c:/finance/account.xls";user id=admin;password=;extended properties=excel 5.0')...xactions 
2、將excel的數(shù)據(jù)導入sql server : 
-- ======================================================
select * into newtable
from opendatasource( 'microsoft.jet.oledb.4.0',
'data source="c:/book1.xls";user id=admin;password=;extended properties=excel 5.0')...[sheet1$]
實例:
select * into newtable
from opendatasource( 'microsoft.jet.oledb.4.0',
'data source="c:/finance/account.xls";user id=admin;password=;extended properties=excel 5.0')...xactions
3、將sql server中查詢到的數(shù)據(jù)導成一個excel文件 
-- ======================================================
t-sql代碼:
exec master..xp_cmdshell 'bcp 庫名.dbo.表名out c:/temp.xls -c -q -s"servername" -u"sa" -p""'
參數(shù):s 是sql服務器名;u是用戶;p是密碼
說明:還可以導出文本文件等多種格式
實例:exec master..xp_cmdshell 'bcp saletesttmp.dbo.cusaccount out c:/temp1.xls -c -q -s"pmserver" -u"sa" -p"sa"'
exec master..xp_cmdshell 'bcp "select au_fname, au_lname from pubs..authors order by au_lname" queryout c:/ authors.xls -c -sservername -usa -ppassword'
在vb6中應用ado導出excel文件代碼:
dim cn as new adodb.connection
cn.open "driver={sql server};server=websvr;database=webmis;uid=sa;wd=123;"
cn.execute "master..xp_cmdshell 'bcp "select col1, col2 from 庫名.dbo.表名" queryout e:/dt.xls -c -sservername -usa -ppassword'"
4、在sql server里往excel插入數(shù)據(jù): 
-- ======================================================
insert into opendatasource( 'microsoft.jet.oledb.4.0',
'data source="c:/temp.xls";user id=admin;password=;extended properties=excel 5.0')...table1 (a1,a2,a3) values (1,2,3)
t-sql代碼:
insert into
opendatasource('microsoft.jet.oledb.4.0',
'extended properties=excel 8.0;data source=c:/training/inventur.xls')...[filiale1$]
(bestand, produkt) values (20, 'test')
總結:利用以上語句,我們可以方便地將sql server、access和excel電子表格軟件中的數(shù)據(jù)進行轉換,為我們提供了極大方便!
新聞熱點
疑難解答
圖片精選