以下是ADO建立数据源和访问数据库的例子,也许对你有用!
<<<<<<<<<<<<<<<<<<<<<<<<code>>>>>>>>>>>>>>>>>>>>>>>>>
Private mvarLastDSN As String
Private mvarConnectResult As Boolean
Public Function GetConnectDSN(ByVal DSN As String) As String
Dim C As DataLinks
Dim d As ADODB.Connection
Set C = New DataLinks
Set d = New ADODB.Connection
d.ConnectionString = DSN
If C.PromptEdit(d) = True Then
GetConnectDSN = d.ConnectionString
mvarLastDSN = d.ConnectionString
mvarConnectResult = True
Else
mvarConnectResult = False
GetConnectDSN = ""
End If
Set d = Nothing
End Function
Public Function ConnectDB(ByVal DSN As String) As String
If DSN <> "" Then
ConnectDB = GetConnectDSN(DSN)
Else
ConnectDB = GetConnectDSN("")
Dim i As Integer
End If
End Function
Public Property Get ConnectResult() As Boolean
ConnectResult = mvarConnectResult
End Property
Public Function OpenConnection(DSN As String) As String
Set CONN = New ADODB.Connection
CONN.ConnectionString = DSN
CONN.CursorLocation = adUseClient
On Error GoTo ErrHandle
CONN.Open
OpenConnection = DSN
Exit Function
ErrHandle:
DSN = ""
DSN = ConnectDB(DSN)
CONN.ConnectionString = DSN
CONN.CursorLocation = adUseClient
CONN.Open
OpenConnection = DSN
End Function
'////////////////////////////////////////////////////////
'//FuctionName: ExecuteSQL
'//功能:执行SQL语句
'//IN: SQLStatement(string)
'//OUT: None
'////////////////////////////////////////////////////////
Public Function ExecuteSQL(SQLStatement As String) As Boolean
Dim comm As ADODB.COMMAND
Set comm = New ADODB.COMMAND
comm.ActiveConnection = CONN
comm.CommandText = SQLStatement
comm.CommandType = adCmdText
comm.Execute
End Function
'////////////////////////////////////////////////////////
'//FunctionName: ExecuteTSQL
'//功能:执行SQL语句打开一个表
'//IN: SQLStatement
'//OUT: rsTable
'////////////////////////////////////////////////////////
Public Function ExecuteTSQL(rsTable As ADODB.Recordset, SQLStatement As String) As Boolean
ExecuteTSQL = False
rsTable.LockType = adLockOptimistic
rsTable.CursorType = adOpenKeyset
On Error GoTo ErrHandle
rsTable.Open SQLStatement, CONN, , , adCmdText
'MsgBox rsTable.RecordCount
ExecuteTSQL = True
Exit Function
ErrHandle:
If Err.Number = -2147467259 Then
MsgBox Err.Number & "====" & Err.Description
Exit Function
End If
End Function