如何使用ADO连接到局内网电脑的数据库?

如何使用ADO连接到局内网电脑的数据库?
最新回答
栀夏暖阳

2022-04-04 18:45:00

VB Code
Sub GetUserList(FileName As Variant, TableNM As String, PassStr As String, Optional OType As String, Optional RowNum As Long, Optional Password As String)
On Error GoTo ErrorHandler
' This sub requires a reference to the Microsoft ActiveX Data Objects 2.x Library
Dim Cnct As String, Src As String
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset
Dim RecI As Integer, RecCnt As Integer
' Open the connection
Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.Jet.OLEDB.4.0; "
Cnct = Cnct & "Data Source=" & FileName & ";Jet OLEDB:Database Password=" & PassStr
'Open a database with password
Connection.Open ConnectionString:=Cnct
Set Recordset = New ADODB.Recordset
Select Case OType
Case "ReadDB"
With Recordset
Src = "SELECT * FROM " & TableNM
.Open Src, Connection, adOpenStatic, adLockReadOnly, adCmdText
ArrUsers = .GetRows
RecCnt = .RecordCount - 1
End With
For RecI = 0 To RecCnt
LogOn.LogUser.AddItem ArrUsers(1, RecI)
Next RecI
Case "UpdateDB"
With Recordset
Src = "SELECT * FROM " & TableNM
.Open Src, Connection, adOpenDynamic, adLockOptimistic
.Move RowNum
.Fields(2).Value = Password
.Update
End With
End Select
Recordset.Close
Connection.Close
Set Recordset = Nothing
Set Connection = Nothing
ErrorHandler:
' clean up
If Not Recordset Is Nothing Then
If Recordset.State = adStateOpen Then Recordset.Close
End If
Set Recordset = Nothing

If Not Connection Is Nothing Then
If Connection.State = adStateOpen Then Connection.Close
End If
Set Connection = Nothing

If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If

End Sub
夜巴黎

2022-04-16 19:32:46

数据源 填入局域网内IP 就可以了。