数据库位于sqlYog,
当我运行此程序时,我收到错误:
CommandText属性尚未正确初始化。?
请帮帮我
Imports MySql.Data.MySqlClient
Imports System.Data
Partial Class login
Inherits System.Web.UI.Page
Public dbconn As New MySqlConnection
Public sql As String
Public dbcomm As New MySqlCommand
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dbconn = New MySqlConnection("data source= localhost; user id=root; password=search; database=bookstore;")
dbconn.Open()
dbcomm.Connection = dbconn
End Sub
Protected Sub btn_login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_login.Click
Try
dbcomm = New MySqlCommand(sql, dbconn)
sql = "select * from bookstore.lib_user where username=@username and password=@password"
dbcomm.Parameters.AddWithValue("@username", txt_username.Text)
dbcomm.Parameters.AddWithValue("@password", txt_password.Text)
Dim dbadpt As New MySqlDataAdapter(dbcomm)
Dim dt As New DataTable()
dbadpt.Fill(dt)
If dt.Rows.Count > 0 Then
MsgBox("login is suceesfull")
Else
Literal1.Text = "invalid login"
End If
Catch ex As Exception
MsgBox("read this error: " + ex.Message)
End Try
End Sub
End Class
答案 0 :(得分:2)
请更改行的顺序。
首先设置查询文本,然后调用命令构造函数。
sql = "select * from bookstore.lib_user where username=@username and password=@password"
dbcomm = New MySqlCommand(sql, dbconn)
答案 1 :(得分:0)
根据您的代码sql
在分配到MySqlCommand
时为空时,您需要先设置sql
值,然后将其分配给MySqlCommand
对象。
sql = "select * from bookstore.lib_user where username=@username and password=@password"
dbcomm = New MySqlCommand(sql, dbconn)