【问题标题】:OleDb data reader issueOleDb 数据读取器问题
【发布时间】:2014-03-28 20:07:43
【问题描述】:

我有一个使用 MySQL 数据库的 vb.net 程序。现在我将数据库更改为Microsoft Access 2010。我更改了所有必要的设置,如连接首选项、命令等。

问题是:我有一个错误

mydata = mycommand.ExecuteReader

它会产生错误:

错误 1 ​​类型“System.Data.OleDb.OleDbDataReader”的值无法转换为“MySql.Data.MySqlClient.OleDb.OleDbDataReader”

整个代码是:

 Public Sub showAlarm()
        Try
            Dim conn As New OleDb.OleDbConnection(connection)
            Dim myadapter As New OleDb.OleDbDataAdapter
            conn.Open()
            Dim sqlquery = "select * from alarm"
            Dim mycommand As New OleDb.OleDbCommand
            mycommand.Connection = conn
            mycommand.CommandText = sqlquery
            myadapter.SelectCommand = mycommand
            Dim mydata As MySqlDataReader
            mydata = mycommand.ExecuteReader
            'If mydata.HasRows = 0 Then
            '    MsgBox("Data Not Found")
            '    TextBox1.Clear()
            'Else
            mydata.Read()
            TextBox1.Text = mydata.Item("subhi")
            TextBox2.Text = mydata.Item("zuhur")
            TextBox3.Text = mydata.Item("aser")
            TextBox4.Text = mydata.Item("megrib")
            TextBox5.Text = mydata.Item("isha")
            'End If
            conn.Close()
        Catch ex As Exception
            MessageBox.Show("There is a problem with your connection!")
        End Try

    End Sub

如何解决这个问题?

【问题讨论】:

  • 是的,兄弟。现在我用整个代码尽可能简短地编辑了我的问题。谢谢!

标签: mysql vb.net ado.net oledb


【解决方案1】:

由于 mycommandOleDbCommand mycommand.ExecuteReader 返回 OleDbDataReadermydataMySqlDataReader。所以将mydata 更改为OleDbDataReader

Dim mydata As OleDbDataReader = mycommand.ExecuteReader()

旁注:你应该使用Using-statement 来确保所有非托管资源都被释放:

Using conn As New OleDb.OleDbConnection(connection)
    Using mycommand As New OleDb.OleDbCommand("select * from alarm", conn)
        conn.Open()
        Using mydata = mycommand.ExecuteReader()
            If mydata.Read Then
                ' ... '
            End If
        End Using
    End Using
End Using

【讨论】:

  • 谢谢老哥,问题解决了。
猜你喜欢
  • 2011-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
相关资源
最近更新 更多