與各數(shù)據(jù)庫(kù)的幾種連接方式_第1頁(yè)
與各數(shù)據(jù)庫(kù)的幾種連接方式_第2頁(yè)
與各數(shù)據(jù)庫(kù)的幾種連接方式_第3頁(yè)
與各數(shù)據(jù)庫(kù)的幾種連接方式_第4頁(yè)
與各數(shù)據(jù)庫(kù)的幾種連接方式_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、、用DAO控件連接數(shù)據(jù)庫(kù)1.與Access2000數(shù)據(jù)庫(kù)連接Private Sub Command1_Click()'也可直接在控件屬性中設(shè)置以下各項(xiàng)但在控件屬性中不能寫(xiě)入密碼'只有在數(shù)據(jù)數(shù)沒(méi)有密碼的情況下可以省略Data1.Refresh Data1.Connect = "Access 2000;"Data1.DatabaseName = App. Path + "chncmadb.mdb"'數(shù)據(jù)庫(kù)沒(méi)有密碼此句可省Data1.Connect = "pwd=123456"'Data1.RecordSou

2、rce = "耕地資源管理單元屬性數(shù)據(jù)表2004"Data1.RecordSource = "select * from 耕地資源管理單元屬性數(shù)據(jù)表2004"Data1.Refreshmove后才能正確顯示記錄個(gè)數(shù)End Sub2.與沒(méi)有密碼的DBF文件數(shù)據(jù)庫(kù)連接Private Sub Command2_Click()Data1.Connect = "dBASE III;"Data1.DatabaseName = App. Path' Data1.RecordSource ="DBF" Data1.Reco

3、rdSource = "select * from dbf"Data1.Refreshmove后才能正確顯示記錄個(gè)數(shù)End Sub3.與沒(méi)有密碼的Excel文件數(shù)據(jù)庫(kù)連接Private Sub Command3_Click()Data1.Connect = "Excel 8.0;"Data1.DatabaseName = App.Path & "EXcel.xls"Data1.RecordSource = "select * from EXcel.xls"Data1.Refreshmove后才能正確顯示記錄

4、個(gè)End Sub二、用DAO代碼連接數(shù)據(jù)庫(kù)'在使用DAO對(duì)象前應(yīng)選定Visual Basic菜單下的工程中的引用了菜單中的Microsoft DAO 3.6 Object Library選項(xiàng),或其它版本1.DAO代碼與Access數(shù)據(jù)庫(kù)連接Private Sub Command1_Click()Dim Db As DatabaseDim Rs As Recordset'以共享、讀寫(xiě)方式打開(kāi)'如果無(wú)密碼最后一個(gè)參數(shù)可以不要SetDb= OpenDatabase(App.Path & "chncmadb.mdb", False, False, &

5、quot;pwd=123456") '不需要move來(lái)更新記錄個(gè)數(shù) 'Set Rs = Db.OpenRecordset("耕地資源管理單元屬性數(shù)據(jù)表2004") '需要move來(lái)更新記錄個(gè)數(shù)Set Rs = Db.OpenRecordset("select * from 耕地資源管理單元屬性數(shù)據(jù)表2004") If Rs.RecordCount > 0 Then Rs.MoveLast Rs.MoveFirstEnd IfEnd Sub2.DAO代碼與沒(méi)有密碼的DBF文件數(shù)據(jù)庫(kù)連接Private Sub Comm

6、and2_Click()Dim Db As DatabaseDim Rs As Recordset'以共享、讀寫(xiě)方式打開(kāi)Set Db = OpenDatabase(App.Path, False, False, "dbase III;") '不需要move來(lái)更新記錄個(gè)數(shù)'Set Rs = Db.OpenRecordset("DBF") 需要move來(lái)更新記錄個(gè)數(shù) Set Rs = Db.OpenRecordset("select * from DBF") If Rs.RecordCount > 0 The

7、n Rs.MoveLast Rs.MoveFirstEnd IfEnd sub3. 'DAO代碼與沒(méi)有密碼的Excel文件數(shù)據(jù)庫(kù)連接Private Sub Command3_Click()Dim Db As DatabaseDim Rs As Recordset'以共享、讀寫(xiě)方式打開(kāi)'如果無(wú)密碼最后一個(gè)參數(shù)可以不要Set Db = OpenDatabase(App.Path & "EXcel.xls", False, False, "Excel 8.0;")'不需要move來(lái)更新記錄個(gè)數(shù) ' Set Rs

8、= Db.OpenRecordset("EXcel.xls") '表格中的工作目錄sheet '需要move來(lái)更新記錄個(gè)數(shù)Set Rs = Db.OpenRecordset("select * from EXcel.xls") '表格中的工作目錄sheet' If Rs.RecordCount > 0 Then Rs.MoveLast Rs.MoveFirstEnd IfEnd Sub三、用ADO控件連接數(shù)據(jù)庫(kù)'也可直接在控件屬性中設(shè)置以下各項(xiàng)1.ADO控件與Access2000數(shù)據(jù)庫(kù)連接Private Su

9、b Command1_Click() '連接有密碼的Access數(shù)據(jù)庫(kù) 'Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "chncmadb1.mdb;Jet OLEDB:DataBase PASSWORD=123456" '連接沒(méi)有密碼的Access數(shù)據(jù)庫(kù) Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Da

10、ta Source=" & App.Path & "chncmadb.mdb;Persist Security Info=False" 'Adodc1.RecordSource = "耕地資源管理單元屬性數(shù)據(jù)表2004" Adodc1.RecordSource = "select * from 耕地資源管理單元屬性數(shù)據(jù)表2004" Adodc1.Refresh Set DataGrid1.DataSource = Adodc1 DataGrid1.RefreshEnd Sub2.'ADO控件與

11、DBF表連接Private Sub Command2_Click() 'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=dBASE Files;DBQ=" & App.Path & "SourceType=DBF;" 'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Extended Prop

12、erties=DSN=Visual FoxPro Tables;UID=;SourceDB=”& app.path &”;SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes;" 'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DSN=dBASE Files;DBQ=”& app.path

13、 &”;DefaultDir=”& app.path &”;DriverId=533;MaxBufferSize=2048;PageTimeout=5;" '能使表名長(zhǎng)度不受限制 Adodc1.ConnectionString = "Provider=MSDASQL.1;Driver=Microsoft Visual Foxpro Driver;SourceDB=" & App.Path & "SourceType=DBF;Locale Identifier=2052" 'Adodc1.R

14、ecordSource = "DBF1" Adodc1.RecordSource = "select * from DBF1" Adodc1.Refresh Set DataGrid1.DataSource = Adodc1 DataGrid1.RefreshEnd Sub3.'ADO控件與Excel表連接Private Sub Command3_Click() '下面一句測(cè)試未能通過(guò) 'Adodc1.ConnectionString = "Data Provider=MSDASQL.1;driver=Microsoft

15、 Excel Driver *.xls);DBQ=" & App.Path & "EXcel.xls" 'Adodc1.ConnectionString="Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DSN=Excel Files;DBQ=" & App.Path & "EXcel.xls;DefaultDir=”&app.path &”;DriverId=790;MaxBufferSize

16、=2048;PageTimeout=5;" Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "EXcel.xls;Extended Properties='Excel 8.0;HDR=Yes'" 'Adodc1.RecordSource = "EXcel.xls" Adodc1.RecordSource =

17、 "select * from EXcel.xls" Adodc1.Refresh Set DataGrid1.DataSource = Adodc1 DataGrid1.RefreshEnd Sub4.'ADO控件與Oracle數(shù)據(jù)庫(kù)連接Private Sub Command4_Click() 'Adodc1.ConnectionString = "Provider=MSDAORA.1;Password=chncmadb;User ID=chncmadb;Data Source=towebserver;Persist Security Info=

18、True"Adodc1.ConnectionString="Provider=OraOLEDB.Oracle.1;Password=chncmadb;Persist Security Info=True;User ID=chncmadb;Data Source=towebserver" 'Adodc1.RecordSource = "T320481TR012004" '表名不能加方括號(hào) Adodc1.RecordSource = "select * from T320481TR012004" Adodc1.R

19、efresh Set DataGrid1.DataSource = Adodc1 DataGrid1.RefreshEnd Sub5.'ADO控件與SQLserver數(shù)據(jù)庫(kù)連接'未測(cè)試Private Sub Command5_Click() Adodc1.ConnectionString = "Provider=SQLOLEDB.1;Password=111;Persist Security Info=True;User ID=111;Initial Catalog=111;Data Source=111" 'Adodc1.RecordSource

20、= "T320481TR012004" Adodc1.RecordSource = "select * from T320481TR012004" Adodc1.Refresh Set DataGrid1.DataSource = Adodc1 DataGrid1.RefreshEnd Sub四、用ADO代碼連接數(shù)據(jù)庫(kù)'在使用ADO對(duì)象前應(yīng)選定Visual Basic菜單下的工程中的引用了菜單中的Microsoft.ActiveX Data Object 2.5 Library選項(xiàng),或其它版本1.'ADO代碼與Access2000數(shù)據(jù)庫(kù)連

21、接Private Sub Command1_Click() Dim AdoCnn As ADODB.Connection Dim AdoRs As ADODB.Recordset Set AdoCnn = New ADODB.Connection Set AdoRs = New ADODB.Recordset AdoCnn.CursorLocation = adUseClient '.open后面的字符串可以參考ADO控件連接.ConnectionString后面的的字符串 AdoCnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data

22、Source=" & App.Path & "chncmadb1.mdb;Jet OLEDB:DataBase PASSWORD=123456"AdoRs.Open "select * from 耕地資源管理單元屬性數(shù)據(jù)表2004", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText Set DataGrid1.DataSource = AdoRs Set AdoRs = Nothing Set AdoCnn = NothingEnd Sub2.'ADO代碼與DBF表

23、連接Private Sub Command2_Click() Dim AdoCnn As ADODB.Connection Dim AdoRs As ADODB.Recordset Set AdoCnn = New ADODB.Connection Set AdoRs = New ADODB.Recordset AdoCnn.CursorLocation = adUseClient '.open后面的字符串可以參考ADO控件連接.ConnectionString后面的的字符串 AdoCnn.Open "Provider=MSDASQL.1;Driver=Microsoft V

24、isual Foxpro Driver;SourceDB=" & App.Path & "SourceType=DBF;Locale Identifier=2052"AdoRs.Open "select * from DBF1", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText Set DataGrid1.DataSource = AdoRs Set AdoRs = Nothing Set AdoCnn = NothingEnd Sub3.'ADO代碼與Excel表

25、連接Private Sub Command3_Click() Dim AdoCnn As ADODB.Connection Dim AdoRs As ADODB.Recordset Set AdoCnn = New ADODB.Connection Set AdoRs = New ADODB.Recordset AdoCnn.CursorLocation = adUseClient '.open后面的字符串可以參考ADO控件連接.ConnectionString后面的的字符串 AdoCnn.Open"Provider=Microsoft.Jet.OLEDB.4.0;Persi

26、st Security Info=False;Data Source=" & App.Path & "EXcel.xls;Extended Properties='Excel 8.0;HDR=Yes'"AdoRs.Open "select * from EXcel.xls", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText Set DataGrid1.DataSource = AdoRs Set AdoRs = Nothing Set AdoCnn = NothingEnd Sub4.'ADO代碼與Oracle數(shù)據(jù)庫(kù)連接Private Sub Command4_Click() Dim AdoCnn As ADODB.Connection Dim AdoRs As ADODB.Recordset Set AdoCnn = New ADODB.Connection Set AdoRs = New ADODB.Recordset AdoCnn.CursorLocation = adUseClient '.open后面的字符串可以參考ADO控件連接.ConnectionString后面的的字符串 AdoCnn.Open "

溫馨提示

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

評(píng)論

0/150

提交評(píng)論