VB连接数据库的密码问题
网上找了一个模板做数据库产品资料的,但是自身不带密码,我需要添加密码访问。例如,我数据库名称是abc.mdb,已经设置访问密码为12345678。请问以下语句怎么添加访问...
网上找了一个模板做数据库产品资料的,但是自身不带密码,我需要添加密码访问。例如,我数据库名称是 abc.mdb,已经设置访问密码为 12345678。请问以下语句怎么添加访问密码这项?
Public Function getrs(ByVal sql As String, ByVal databasename As String) As adodb.Recordset
Dim con As adodb.Connection
Dim rs As adodb.Recordset
Dim strconnection As String
Dim strarray() As String
Set con = New adodb.Connection
Set rs = New adodb.Recordset
On Error GoTo getrs_error
strconnection = "Provider=Microsoft.jet.oledb.4.0;Data Source=" & App.Path & "\"
strconnection = strconnection & databasename & ".mdb"
strarray = Split(sql)
con.Open strconnection
rs.Open (sql), con, adOpenKeyset, adLockOptimistic
Set getrs = rs
getrs_exit:
Set rs = Nothing
Set con = Nothing
Exit Function
getrs_error:
MsgBox "连接错误" & Err.Description
Resume getrs_exit
End Function
100悬赏奉上,请笑纳………………
200悬赏 展开
Public Function getrs(ByVal sql As String, ByVal databasename As String) As adodb.Recordset
Dim con As adodb.Connection
Dim rs As adodb.Recordset
Dim strconnection As String
Dim strarray() As String
Set con = New adodb.Connection
Set rs = New adodb.Recordset
On Error GoTo getrs_error
strconnection = "Provider=Microsoft.jet.oledb.4.0;Data Source=" & App.Path & "\"
strconnection = strconnection & databasename & ".mdb"
strarray = Split(sql)
con.Open strconnection
rs.Open (sql), con, adOpenKeyset, adLockOptimistic
Set getrs = rs
getrs_exit:
Set rs = Nothing
Set con = Nothing
Exit Function
getrs_error:
MsgBox "连接错误" & Err.Description
Resume getrs_exit
End Function
100悬赏奉上,请笑纳………………
200悬赏 展开
1个回答
展开全部
这两种分别试一下吧:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xxxmydatabase.mdb;User Id=admin;Password=;
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xxxmydatabase.mdb;Jet OLEDB:Database Password=MyDbPassword;
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xxxmydatabase.mdb;User Id=admin;Password=;
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xxxmydatabase.mdb;Jet OLEDB:Database Password=MyDbPassword;
更多追问追答
追问
不行,我需要在这段语句中添加访问密码的方式。
追答
strconnection = "Provider=Microsoft.jet.oledb.4.0;Jet OLEDB:Database Password=MyDbPassword;Data Source=" & App.Path & "\"
strconnection = strconnection & databasename & ".mdb"
MyDbPassword 换成你的数据库密码。
不太明白你的意思,如果密码固定,可以直接写成以上语句。如果密码不固定,可能需要把密码做成变量,然后在调用getrs时传入密码
Public Function getrs(ByVal sql As String, ByVal databasename As String, ByVal pwd As String) As adodb.Recordset
'中间省略
strconnection = "Provider=Microsoft.jet.oledb.4.0;Jet OLEDB:Database Password=" & pwd & ";Data Source=" & App.Path & "\"
strconnection = strconnection & databasename & ".mdb"
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询