在VB.NET 2005中使用多个datareader,在MySQL 5.0中使用C#.NET 2005作为后端

时间:2009-03-28 05:10:23

标签: c# mysql vb.net

我使用MySQL 5.0作为后端。我在我的项目中使用MySqlData.dll。当我尝试执行以下代码时,

尝试             Dim cnData As New MySql.Data.MySqlClient.MySqlConnection             Dim lcmd,lcmd1 As New MySql.Data.MySqlClient.MySqlCommand             Dim ldr,ldr1 As MySql.Data.MySqlClient.MySqlDataReader

        cnData.ConnectionString = "SERVER=localhost;Uid=root;Pwd=root;Database=mytestdatabase"
        cnData.Open()
        lcmd = New MySql.Data.MySqlClient.MySqlCommand("select uom_id,uom_name from uom", cnData)
        ldr = lcmd.ExecuteReader
        While ldr.Read
            lcmd1 = New MySql.Data.MySqlClient.MySqlCommand("select uom_id,uom_name from uom1 where uom_id=" & ldr.Item("uom_id") & "", cnData)
            ldr1 = lcmd1.ExecuteReader
            While ldr1.Read
                MsgBox(ldr1.Item("uom_id"))
                MsgBox(ldr1.Item("uom_name"))
            End While
            ldr1.Close()
        End While
        ldr.Close()
        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

我收到错误消息“已经存在与此Connection关联的OpenDataReader,必须先关闭它。”我必须使用重叠的多个datareader。我哪里出错了?任何人都可以帮助我。这对我很有帮助。

Sivakumar.P

1 个答案:

答案 0 :(得分:1)

此代码与您尝试执行的操作相同:

Try
    Dim cnData As New MySql.Data.MySqlClient.MySqlConnection
    Dim lcmd As MySql.Data.MySqlClient.MySqlCommand
    Dim ldr As MySql.Data.MySqlClient.MySqlDataReader

    cnData.ConnectionString = "SERVER=localhost;Uid=root;Pwd=root;Database=mytestdatabase"
    cnData.Open()
    lcmd = New MySql.Data.MySqlClient.MySqlCommand("select uom1.uom_id, uom1.uom_name from uom join uom1 ON uom.uom_id = uom1.uom_id ORDER BY uom_id", cnData)
    ldr = lcmd.ExecuteReader
    While ldr.Read
        MsgBox(ldr.Item("uom_id"))
        MsgBox(ldr.Item("uom_name"))
    End While
    ldr.Close()
    cnData.Close()

Catch ex As Exception
    MsgBox(ex.Message)
End Try