ExecuteReader CommandText 属性未正确初始化 [英] ExecuteReader CommandText property has not been properly initialized

查看:40
本文介绍了ExecuteReader CommandText 属性未正确初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先很抱歉,如果某些代码不正确.我还是个新手,在 vb.net 上使用 sql

First of all sorry if some of the code isn't right. I'm still new to using sql on vb.net

我有以下代码:

Imports MySql.Data.MySqlClient
Imports System.Data.SqlClient

Public Class Form1

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim objConn As MySqlConnection
        Dim objDataset As New DataSet
        Dim objDataAdapter As MySqlDataAdapter
        Dim myCommand As MySqlCommand

        Dim sqlConn As String

        objConn = New MySqlConnection("server=localhost;userid=root;database=attendance_system")
        myCommand = objConn.CreateCommand

        objConn.Open()
        Dim objReader As MySqlDataReader = myCommand.ExecuteReader

        sqlConn = "SELECT student_name FROM profile"
        objDataAdapter = New MySqlDataAdapter(sqlConn, objConn)
        objDataAdapter.Fill(objDataset, "profile")

        MsgBox("The Connection is Now 'OPEN'")

        objReader.Read()

        TextBox1.Text = objReader("student_name")

        objReader.Close()
        objConn.Close()
    End Sub
End Class

我在 phpmyadmin 中通过 vb.net 使用 MySQL 连接器,并设置了一个包含记录的数据库.连接字符串正在工作,但我的问题是当我尝试单击按钮以在文本框中加载数据时,我不断收到:

I am using MySQL connector via vb.net in phpmyadmin and have set a database with records. The connection string is working, but my problem is when I try to click the button to load the data in the textbox, I keep getting:

CommandText 属性未正确初始化."

The CommandText property has not been properly initialized."

错误在这一行:

"Dim objReader As MySqlDataReader = myCommand.ExecuteReader"

我已经尝试了很多在本网站和其他网站上找到的修复方法.

I've tried a lot of fixes that I've found on this site and the others as well.

推荐答案

这是问题所在:

Dim objReader As MySqlDataReader = myCommand.ExecuteReader

sqlConn = "SELECT student_name FROM profile"

在您尝试执行查询之后声明了 SQL(即使这样您也没有将其设置为命令的 SQL).你希望它如何工作?此外,对于声明 SQL 的变量来说,sqlConn 是一个非常奇怪的名称 - 我希望它是一个 连接.

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:

  • 直接从读者那里阅读
  • 用数据适配器填充DataSet

你不应该那样混合它们.确定您实际上想要做什么,取出与其他样式相关的所有代码,然后确保您按照合理的顺序进行所有操作.

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.

这篇关于ExecuteReader CommandText 属性未正确初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆