2014-03-27 110 views
1

我有一個使用MySQL數據庫的vb.net程序。現在我將數據庫更改爲Microsoft Access 2010。我變了樣連接優先所有必要的設置,命令等OleDb數據讀取器問題

的問題是:我有一個錯誤周圍

mydata = mycommand.ExecuteReader

,併產生一個錯誤:

Error 1 Value of type 'System.Data.OleDb.OleDbDataReader' cannot be converted to '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 

如何解決這個問題?

+0

是您正確的兄弟。現在我已經用整個代碼儘可能簡短地編輯了我的問題。謝謝! – ensberiyu

回答

2

由於mycommandOleDbCommandmycommand.ExecuteReader返回OleDbDataReadermydataMySqlDataReader。因此,將mydata更改爲OleDbDataReader

Dim mydata As OleDbDataReader = mycommand.ExecuteReader() 

側面說明:你應該使用Using語句來確保所有非託管資源配置:

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 
+0

感謝兄弟,問題已解決。 – ensberiyu