没有为Visual Studio 2013提供一个或多个参数的值

问题描述 投票:0回答:1

我检查了我的拼写,他们还好这是我的“更新”代码

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    TextBox3.Text = TextBox6.Text - TextBox2.Text
    pro = "provider = Microsoft.ACE.OLEDB.12.0;Data Source=D:\chemz high school fees management system\chemz_db.accdb "
    connstring = pro
    myconnection.ConnectionString = connstring
    myconnection.Open()
    command = "update SCHOOL_FEES set [Fees]='" & TextBox6.Text & "',[Fees_paid]='" & TextBox2.Text & "',[Fees_owed]='" & TextBox3.Text & "',[Practical_fee]='" & TextBox4.Text & "',[Sports_fee]='" & TextBox5.Text & "'where[Learner_ID]=" & TextBox1.Text & ""
    Dim cmd As OleDbCommand = New OleDbCommand(command, myconnection)
    MsgBox("UPDATED")
    Try
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        myconnection.Close()
        TextBox1.Clear()
        TextBox6.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
    Catch ex As Exception
        MsgBox(ex.Message)
        MsgBox("UPDATE FAILED")

    End Try
End Sub

结束类

vb.net visual-studio-2013
1个回答
0
投票

我已经向您展示了如何使用参数。在Access(OleDb)中,参数在命令文本中出现的顺序必须与它们添加到参数集合的顺序匹配。

使用...结束使用块将确保即使有错误,连接和命令也将被关闭和处置。

Private Sub OPCode()
    Dim Command = "update SCHOOL_FEES set [Fees]= @Fee, [Fees_paid]= @FeePaid, [Fees_owed]= @FeeOwed, [Practical_fee]= @PracticalFee, [Sports_fee]= @SportsFee where [Learner_ID]= @ID;"

    Using myconnection As New OleDbConnection("provider = Microsoft.ACE.OLEDB.12.0;Data Source=D:\chemz high school fees management system\chemz_db.accdb "),
            cmd As New OleDbCommand(Command, myconnection)
        With cmd.Parameters
            .Add("@Fee", OleDbType.Decimal).Value = CDec(TextBox6.Text)
            .Add("@FeePaid", OleDbType.Decimal).Value = CDec(TextBox2.Text)
            .Add("@FeeOwed", OleDbType.Decimal).Value = CDec(TextBox3.Text)
            .Add("@PracticalFee", OleDbType.Decimal).Value = CDec(TextBox4.Text)
            .Add("@SportsFee", OleDbType.Decimal).Value = CDec(TextBox5.Text)
            .Add("@ID", OleDbType.Integer).Value = CInt(TextBox1.Text)
        End With
        Try
            myconnection.Open()
            cmd.ExecuteNonQuery()
            MsgBox("UPDATED")
        Catch ex As Exception
            MsgBox(ex.Message)
            MsgBox("UPDATE FAILED")
        End Try
    End Using
    ClearTextBoxes()
End Sub

Private Sub ClearTextBoxes()
    TextBox1.Clear()
    TextBox6.Clear()
    TextBox2.Clear()
    TextBox3.Clear()
    TextBox4.Clear()
    TextBox5.Clear()
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.