ExecuteReader CommandText property has not been properly initialized

对着背影说爱祢 提交于 2019-12-02 01:54:26

This is the problem:

Dim objReader As MySqlDataReader = myCommand.ExecuteReader

sqlConn = "SELECT student_name FROM profile"

You're declared the SQL after you've tried executing the query (and even then you don't set it as the SQL for the command). How would you expect that to work? Additionally, sqlConn is a very strange name for a variable declaring the SQL - I'd expect it to be a connection.

It looks like you're trying to mix too very different ways of fetching data:

  • Reading directly from the reader
  • Filling a DataSet with a data adapter

You shouldn't be mixing them like that. Work out which you actually want to do, take out all the code related to the other style, and then make sure you're doing everything in a sensible order.

From MySqlCommand Class and the example given as

Public Sub ReadMyData(myConnString As String)
  Dim mySelectQuery As String = "SELECT * FROM Test.Dept"
  Dim myConnection As New MySqlConnection(myConnString)
  Dim myCommand As New MySqlCommand(mySelectQuery, myConnection)
  myConnection.Open()
  Dim myReader As MySqlDataReader = myCommand.ExecuteReader()
  Try
    While myReader.Read()
      Console.WriteLine(myReader.GetInt32(0).ToString() + ", " _
        + myReader.GetString(1))
    End While
  Finally
      ' always call Close when done reading.
      myReader.Close()
      ' always call Close when done with connection.
      myConnection.Close()
  End Try
End Sub

Your command object is missing the select statement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!