VB6中的空Datagrid问题

时间:2010-05-18 14:59:38

标签: vb6 datagrid ado

最近,我遇到了一个问题;当我将记录集绑定到datagrid,并运行应用程序时,即使记录集有数据,也不会填充数据网格

我使用以下代码

Option Explicit
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim recordset As New ADODB.recordset





Private Sub InitializeConnection()
Dim str As String

str = _
   "Provider=Microsoft.Jet.OLEDB.4.0;" & _
   "Data Source=" + App.Path + "\phonebook.mdb;" & _
   "Persist Security Info=False"

    conn.CursorLocation = adUseClient

    conn.ConnectionString = str

    conn.Open (conn.ConnectionString)


End Sub
Private Sub AbandonConnection()
    If conn.State <> 0 Then
        conn.Close
    End If
End Sub
Private Sub Persons_Read()

    Dim qry_all As String

   ' qry_all = "select * from person,web,phone Where web.personid = person.id And phone.personid = person.id"
    qry_all = "SELECT * FROM person"

    Call InitializeConnection

    cmd.CommandText = qry_all
    cmd.CommandType = adCmdText

    Set cmd.ActiveConnection = conn


    If conn.State = 1 Then

       Set recordset = cmd.Execute()

    End If

    Call BindDatagrid


    Call AbandonConnection


End Sub
Private Function Person_Add()

End Function
Private Function Person_Delete()

End Function
Private Function Person_Update()

End Function
Private Sub BindDatagrid()



   Set dg_Persons.DataSource = recordset


    dg_Persons.Refresh
End Sub

Private Sub cmd_Add_Click()
    Person_Add
End Sub

Private Sub cmd_Delete_Click()
    Person_Delete
End Sub

Private Sub cmd_Update_Click()
    Person_Update
End Sub

Private Sub Form_Load()
    Call Persons_Read

End Sub

Private Sub mnu_About_Click()
    frm_About.Show

End Sub

提前致谢

2 个答案:

答案 0 :(得分:1)

对我来说,通过在设置连接超时后将此行添加到我的代码中,我能够解决问题

conn.CursorLocation = adUseClient

这解决了我的问题

答案 1 :(得分:0)

通过在查询后没有关闭连接来解决这个问题!