无法在ADO中连接和查询

时间:2011-03-16 09:06:10

标签: sql-server ms-access vb6 ado

我正在创建一个.MDB文件,其中包含ms访问数据库和使用vb 6创建的表单。我正在使用ms access 2000,我需要连接到MDB中的本地数据库和远程MS SQL 2005年数据库。

在下面的代码中,我可以使用msgbox来显示结果集中的值返回,但是当尝试在textBox中输出它时,例如:txtStatus.Value = txtStatus.Value & rstRecordSet.Fields(1) & vbCrLf,它只是挂起。并且在教程的原始示例中显示的方法得到了一个Debug.Print的方法,但事实证明并没有做任何我能看到的事情。我的意思是,VB没有控制台面板,print语句将转到哪里?

带错误的代码:

    Function Testing()
On Error GoTo Error_Handling
   Dim conConnection As New ADODB.Connection
   Dim cmdCommand As New ADODB.Command
   Dim rstRecordSet As New ADODB.Recordset

   conConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
   App.Path & "\" & CurrentDb.Name & ";"
   conConnection.CursorLocation = adUseClient

   With cmdCommand
    .ActiveConnection = conConnection
    .CommandText = "SELECT * FROM Opt_In_Customer_Record;"
    .CommandType = adCmdText
   End With

   With rstRecordSet
    .CursorType = adOpenStatic
    .CursorLocation = adUseClient
    .LockType = adLockOptimistic
    .Open cmdCommand
   End With

   If rstRecordSet.EOF = False Then
        rstRecordSet.MoveFirst
        Do
            MsgBox "Record " & rstRecordSet.AbsolutePosition & " " & _
          rstRecordSet.Fields(0).Name & "=" & rstRecordSet.Fields(0) & " " & _
          rstRecordSet.Fields(1).Name & "=" & rstRecordSet.Fields(1)
          rstRecordSet.MoveNext
        Loop Until rstRecordSet.EOF = True
   End If

   conConnection.Close
   Set conConnection = Nothing
   Set cmdCommand = Nothing
   Set rstRecordSet = Nothing

   Exit Function

Error_Handling:
MsgBox "Error during function Testing!"
Exit Function

End Function

1 个答案:

答案 0 :(得分:1)

我认为这开头是一个笑话;-) 无论如何,我假设你正在谈论ADO,就像你的标题一样。

Here you can查找内容。 这个site将帮助您处理不同数据库的连接字符串 访问和使用ADO的SQL Server之间的区别正是连接字符串。 我建议你避免远程数据控制因为一开始就让你的生活变得更简单但是你必须为它们而努力,因为它们不能正常工作。

这是连接和获取数据的示例:

Dim cnn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim strSql As String

cnn.ConnectionString = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=m:\testdbSource\testSource.mdb;" & _
    "User Id=admin;Password=;"
cnn.Open

cmd.ActiveConnection = cnn
cmd.CommandType = adCmdText
cmd.CommandText = "select * from tblSource"
cmd.Execute

Set cmd = Nothing
cnn.Close
Set cnn = Nothing

此示例适用于我:

Function Testing()

    On Error GoTo Error_Handling

    Dim MyDb As String
    Dim conConnection As New ADODB.Connection
    Dim cmdCommand As New ADODB.Command
    Dim rstRecordSet As New ADODB.Recordset

    MyDb = "db1.mdb"

    With conConnection
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = App.Path & "\" & MyDb
        .Open
    End With


    With cmdCommand
        .ActiveConnection = conConnection
        .CommandText = "SELECT * FROM Opt_In_Customer_Record;"
        .CommandType = adCmdText
    End With

   With rstRecordSet
    .CursorType = adOpenStatic
    .CursorLocation = adUseClient
    .LockType = adLockOptimistic
    .Open cmdCommand
   End With

   Do While Not rstRecordSet.EOF
        MsgBox "Record " & rstRecordSet.AbsolutePosition & " " & _
          rstRecordSet.Fields(0).Name & "=" & rstRecordSet.Fields(0) & " " & _
          rstRecordSet.Fields(1).Name & "=" & rstRecordSet.Fields(1)
          rstRecordSet.MoveNext
   Loop

   conConnection.Close
   Set conConnection = Nothing
   Set cmdCommand = Nothing
   Set rstRecordSet = Nothing

   Exit Function

Error_Handling:
    MsgBox "Error during function Testing!"
    MsgBox Err.Description

End Function