明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

如何設(shè)置對(duì)VB數(shù)據(jù)庫(kù)連接的動(dòng)態(tài)路徑

[摘要]我個(gè)人因?yàn)榻?jīng)常作一些數(shù)據(jù)庫(kù)方面的程序,對(duì)于程序間如何與數(shù)據(jù)庫(kù)進(jìn)行接口的問(wèn)題之煩是深有體會(huì),因?yàn)閂B在數(shù)據(jù)庫(kù)鏈接的時(shí)候,一般是靜態(tài),即數(shù)據(jù)庫(kù)存放的路徑是固定的,如用VB的DATA,adodc,DataEnvironment 等到作數(shù)據(jù)庫(kù)鏈接時(shí),如果存放數(shù)據(jù)庫(kù)的路徑被改變的話,就會(huì)找不到路經(jīng),真是一個(gè)...
我個(gè)人因?yàn)榻?jīng)常作一些數(shù)據(jù)庫(kù)方面的程序,對(duì)于程序間如何與數(shù)據(jù)庫(kù)進(jìn)行接口的問(wèn)題之煩是深有體會(huì),因?yàn)閂B在數(shù)據(jù)庫(kù)鏈接的時(shí)候,一般是靜態(tài),即數(shù)據(jù)庫(kù)存放的路徑是固定的,如用VB的DATA,adodc,DataEnvironment 等到作數(shù)據(jù)庫(kù)鏈接時(shí),如果存放數(shù)據(jù)庫(kù)的路徑被改變的話,就會(huì)找不到路經(jīng),真是一個(gè)特別煩的事。
筆者的解決方法是利用app.path 來(lái)解決這個(gè)問(wèn)題。
一、用data控件進(jìn)行數(shù)據(jù)庫(kù)鏈接,可以這樣:
在form_load()過(guò)程中放入:
private form_load()
Dim str As String 注釋:定義
str = App.Path
If Right(str, 1) <> "\" Then
str = str + "\"
End If
data1.databasename=str & "\數(shù)據(jù)庫(kù)名"
data1.recordsource="數(shù)據(jù)表名"
data1.refresh
sub end
這幾句話的意為,打開(kāi)當(dāng)前程序運(yùn)行的目錄下的數(shù)據(jù)庫(kù)。
你只要保證你的數(shù)據(jù)庫(kù)在你程序所在的目錄之下就行了。

二、利用adodc(ADO Data Control)進(jìn)行數(shù)據(jù)庫(kù)鏈接:
private form_load ()
Dim str As String 注釋:定義
str = App.Path
If Right(str, 1) <> "\" Then
str = str + "\"
End If
str = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & str & "\tsl.mdb"
Adodc1.ConnectionString = str
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "select * from table3"
Adodc1.Refresh
end sub

三、利用DataEnvironment進(jìn)行數(shù)據(jù)庫(kù)鏈接
可在過(guò)程中放入:
On Error Resume Next
If DataEnvironment1.rsCommand1.State <> adStateClosed Then
DataEnvironment1.rsCommand1.Close 注釋:如果打開(kāi),則關(guān)閉
End If
注釋:i = InputBox("請(qǐng)輸入友人編號(hào):", "輸入")
注釋:If i = "" Then Exit Sub
DataEnvironment1.Connection1.Open App.Path & "\userdatabase\tsl.mdb"
DataEnvironment1.rsCommand1.Open "select * from table3 where 編號(hào)=注釋:" & i & "注釋:"
注釋:Set DataReport2.DataSource = DataEnvironment1
注釋:DataReport2.DataMember = "command1"
注釋:DataReport2.show
end sub

四、利用ADO(ActiveX Data Objects)進(jìn)行編程:
建立連接:
dim conn as new adodb.connection
dim rs as new adodb.recordset
dim str
str = App.Path
If Right(str, 1) <> "\" Then
str = str + "\"
End If
str = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & str & "\tsl.mdb"
conn.open str
rs.cursorlocation=aduseclient
rs.open "數(shù)據(jù)表名",conn,adopenkeyset.adlockpessimistic
用完之后關(guān)閉數(shù)據(jù)庫(kù):
conn.close
set conn=nothing