VBA連接SQLSERVER數(shù)據(jù)庫實(shí)例_第1頁
VBA連接SQLSERVER數(shù)據(jù)庫實(shí)例_第2頁
VBA連接SQLSERVER數(shù)據(jù)庫實(shí)例_第3頁
VBA連接SQLSERVER數(shù)據(jù)庫實(shí)例_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、vba中用ado訪問sql server數(shù)據(jù)庫:數(shù)據(jù)查詢?yōu)榱烁玫慕忉?,特提供兩個實(shí)例:使用數(shù)據(jù)對象ado訪問sql數(shù)據(jù)庫,從而進(jìn)行數(shù)據(jù)查詢,并將查詢的數(shù)據(jù)返回到excel中,由于此程序需用到sql數(shù)據(jù)庫,所以只供大家參考,不便運(yùn)行,但大家可以看出其精華!=實(shí)例1:= private sub commandbutton1_click() dim cn as new adodb.connectiondim rst as new adodb.recordsetdim r, c, f, i as integerdim sql_text, day1, linenumber, box as string

2、const cnnstr = provider = sqloledb; & _ data source = apsgszml04; & _ initial catalog = bhl2ken;user id =sa;password =;連接數(shù)據(jù)庫 默認(rèn)sql server服務(wù)器:local 用戶:sa 密碼:空 數(shù)據(jù)庫:northwind表源:customersday1 = userform1.textbox1.textlinenumber = userform1.combobox1.textbox = userform1.combobox2.textcn.open cnnstrsql_te

3、xt = sql_text & select convert(char,dbo.try123.day,101) as date1, sql_text = sql_text & dbo.try123.linenumber,dbo.try123.box_no,dbo.try123.serialnumber,dbo.try123.lotnumber sql_text = sql_text & from dbo.try123 sql_text = sql_text & where (convert(char,dbo.try123.day,101)= & day1 & and dbo.try123.li

4、nenumber= & linenumber & and dbo.try123.box_no= & box & ) sql_text = sql_text & order by dbo.try123.serialnumber 使用sql數(shù)據(jù)庫查詢語言查詢 select convert(char,dbo.try123.day,101) as date1,dbo.try123.linenumber,dbo.try123.box_no,dbo.try123.serialnumber,dbo.try123.lotnumberfrom dbo.try123where (convert(char,dbo.

5、try123.day,101)= & day1 & and dbo.try123.linenumber= & linenumber & and dbo.try123.box_no= & box & order by dbo.try123.serialnumberrst.open sql_text, cn, adopenstatic, adlockbatchoptimistic 用adopenstatic + adlockbatchoptimistic打開較快,且占用資源小r = 5 excel表的行序號c = 3 excel表的列序號i = 0 sql表的字段序號f = rst.fields.

6、count - 1worksheets(sheet1).unprotectworksheets(sheet1).cells.clearcontentswhile not rst.eof for i = 0 to f sheet1.cells(r, i + 3).rows.value = rst.fields(i).value next i r = r + 1 rst.movenext 將數(shù)據(jù)庫的數(shù)據(jù)返回到excel表中wendworksheets(sheet1).protectuserform1.hidemsgbox (讀取完畢)rst.close 完成后要關(guān)閉cn.close 完成后要關(guān)閉注

7、意:以上為直接連接sql server 的方法,不用建odbc數(shù)據(jù)源worksheets(sheet1).activateend sub=實(shí)例2:=sub test() 定義過程名稱dim i as integer, j as integer, sht as worksheet i,j為整數(shù)變量;sht 為excel工作表對象變量,指向某一工作表dim cn as new adodb.connection 定義數(shù)據(jù)鏈接對象 ,保存連接數(shù)據(jù)庫信息;請先添加ado引用dim rs as new adodb.recordset 定義記錄集對象,保存數(shù)據(jù)表dim strcn as string ,st

8、rsql as string 字符串變量strcn = provider=sqloledb;server=服務(wù)器名稱或ip地址;database=數(shù)據(jù)庫名稱;uid=用戶登錄名;pwd=密碼; 定義數(shù)據(jù)庫鏈接字符串下面的語句將讀取數(shù)據(jù)表數(shù)據(jù),并將它保存到excel工作表中:畫兩張表想像一下,工作表為一張兩維表,記錄集也是一張兩維表strsql = select 字段1,字段2 from 表名稱 定義sql查詢命令字符串cn.open strcn 與數(shù)據(jù)庫建立連接,如果成功,返回連接對象cnrs.open strsql, cn 執(zhí)行strsql所含的sql命令,結(jié)果保存在rs記錄集對象中i =

9、1set sht = thisworkbook.worksheets(sheet1) 把sht指向當(dāng)前工作簿的sheet1工作表do while not rs.eof 當(dāng)數(shù)據(jù)指針未移到記錄集末尾時,循環(huán)下列操作 sht.cells(i, 1) = rs(字段1) 把當(dāng)前記錄的字段1的值保存到sheet1工作表的第i行第1列 sht.cells(i, 2) = rs(字段2) 把當(dāng)前字段2的值保存到sheet1工作表的第i行第2列 rs.movenext 把指針移向下一條記錄 i = i + 1 i加1,準(zhǔn)備把下一記錄相關(guān)字段的值保存到工作表的下一行l(wèi)oop 循環(huán)rs.close 關(guān)閉記錄集,至

10、此,程序?qū)涯硵?shù)據(jù)表的字段1和字段2保存在excel工作表sheet1的第1、2列,行數(shù)等于數(shù)據(jù)表的記錄數(shù)下面的語句將讀取excel工作表數(shù)據(jù),并將之簡單計(jì)算后存入數(shù)據(jù)庫,這里使用上面程序中的一些變量假設(shè)分別讀取工作表sheet1第5行至第500行的第8列和第9列已存在的數(shù)據(jù),然后將它們相乘,并將積存入數(shù)據(jù)庫的某個表strsql= 清空上面定義的變量for i=5 to 500 循環(huán)開始,i從5到500s strsql=strsql & insert into 表名(字段) values( & sht.cells(i,8)*sht.cells(i,9) & ) ; 構(gòu)造sql命令串next至此生成一串sql命令串,保存的內(nèi)容大概為:insert int

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論