CommandText属性尚未正确初始化

时间:2014-02-18 14:17:11

标签: vb.net executenonquery

我使用vb.net和mysqladmin作为我的数据库。

我的代码有问题,我不知道如何调试它。 请帮帮我..

我的问题是当我点击按钮更新错误显示
“CommandText属性尚未正确初始化。”

这是代码:

 Dim intDB_ID_Selected As Integer


'Private Sub cmdupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupdate.Click



If MessageBox.Show("Do you want to update this record?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then

 Dim sqlcommand As New MySqlCommand("UPDATE user_info " & _
                                 " SET name = '" & txtname.Text.Trim & "'," & _
                                 " address = '" & txtaddress.Text.Trim & "', " & _
                                 " age = '" & txtage.Text.Trim & "', " & _
                                 " WHERE id= '" & intDB_ID_Selected & "'", sConnection)
        Call execCmd(SQL)
        load1()
        MsgBox("Record updated successfully.", MsgBoxStyle.Information)
    End If
End Sub `


 Public Sub execCmd(ByVal PstrSQL As String)
    With cmd
        .CommandText = PstrSQL
        .ExecuteNonQuery()
    End With
End Sub

错误行在

.ExecuteNonQuery()

我是这种语言的初学者,所以请帮助我。我乞求你们!!

2 个答案:

答案 0 :(得分:0)

什么是cmd并且已经初始化了?

哦,并尝试使用参数化查询。你越早养成这个习惯就越好。

答案 1 :(得分:0)

在选择消息框后尝试执行此操作。 cmd未在execCmd中正确初始化。

Dim sqlStr as String = "UPDATE user_info " & _
                       " SET name = '" & txtname.Text.Trim & "'," & _
                       " address = '" & txtaddress.Text.Trim & "', " & _
                       " age = '" & txtage.Text.Trim & "', " & _
                       " WHERE id= '" & intDB_ID_Selected & "'", sConnection)

Dim sqlcommand As New MySqlCommand(sqlStr)
sqlcommand.ExecuteNonQuery()

load1()
MsgBox("Record updated successfully.", MsgBoxStyle.Information)