未设置命令文本

时间:2017-11-07 14:43:47

标签: sql .net vb.net

美好的一天我遇到一个错误,指出我尝试添加数据时没有设置命令文本。

 Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click    
        Try
            connString = (dbsource)
            myConnection.ConnectionString = connString
            myConnection.Open()
            Dim str As String    
            str = "Add [Bill] set [Log_ID] =  ' " & TextBox2.Text & " ' where [Bill_ID] = " & TextBox1.Text & " " 
            Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
        Catch err As Exception
            MsgBox(err.Message)    
        End Try
        Try
            Dim str As String
            Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            myConnection.Close()
            TextBox1.Clear()
            TextBox2.Clear()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try 
    End Sub

1 个答案:

答案 0 :(得分:0)

该代码中有两个Try / Catch块。每个Try / Catch块都有名为strcmd的变量。但是,这两个不同的 str变量和两个不同的 cmd变量。 Try / Catch块每个都定义了它们自己的作用域,只是在新作用域中重新声明一个具有相同名称的新变量并不能为该变量赋予与之前作用域相同的值。

此处还有一些其他内容表明您可能有使用vb6 / vbscript时代代码的经验,或者具有旧代码经验的教师。 VB.Net是一种全新的动物,具有新的语言习语。你想要更像这样的东西:

'Separate your Database code from your user interface code
Public Sub SetBillLogItem(LogID As String, BillID As String)
    'Don't bother with Try/Catch in this method.
    'Let exceptions bubble up to be handled in the presentation layer

    ' What database are you using? "Add" isn't valid SQL
    ' Also, notice how this string is a constant.
    Dim sql As String = "Add [Bill] set [Log_ID] = ? where [Bill_ID] = ? "

    'Using block guarantees the connection is closed properly even if an exception is thrown
    ' Also, .Net **strongly** prefers you to create a new connection object for most DB operations
    Using cn  As New OleDbConnection(dbsource), _
          cmd As New OleDBCommand(sql, cn)

        'ALWAYS use parameters to set values for your SQL. 
        'Guessing a parameter type and length here, you might want Integer instead
        cmd.Parameters.Add("LogId", OleDbType.VarWChar, 10).Value = LogId
        cmd.Parameters.Add("BillID", OleDbType.VarWChar, 10).Value = BillID

        cn.Open()
        cmd.ExecuteNonQuery()
        'No need to call cn.Close(), Using block takes care of this for us in a safer way
    End Using
End Sub

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click    
    Try
        SetBillLogItem(TextBox2.Text, TextBox1.Text)
    Catch err As Exception
        MsgBox(err.Message)    
    End Try 
End Sub

再一次没有我的额外评论,所以你可以看到这不再是你已有的代码:

Public Sub SetBillLogItem(LogID As String, BillID As String)
    Dim sql As String = "Add [Bill] set [Log_ID] = ? where [Bill_ID] = ? "
    Using cn  As New OleDbConnection(dbsource), _
          cmd As New OleDBCommand(sql, cn)

        cmd.Parameters.Add("LogId", OleDbType.VarWChar, 10).Value = LogId
        cmd.Parameters.Add("BillID", OleDbType.VarWChar, 10).Value = BillID

        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click    
    Try
        SetBillLogItem(TextBox2.Text, TextBox1.Text)
    Catch err As Exception
        MsgBox(err.Message)    
    End Try 
End Sub