简体   繁体   中英

What is the code for VB.Net to delete and update data in MySQL ? (Use parameterized Query)

What is the code for VB.Net to delete and update data in MySQL ? (Use parameterized Query)

This is my code for Update :

Dim Query As String

    Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=test;User ID=root;Password=;")
    con.Open()

    Query = "UPDATE userreg SET UserName ='" + txtUserName.Text + "',"
    Query = Query + "Age = " + txtAge.Text
    Query = Query + " WHERE idUserReg = " + txtUserRegId.Text

    Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
    MsgBox(Query)
    Dim i As Integer = cmd.ExecuteNonQuery()
    If (i > 0) Then
        lblMsg.Text = "Record is Successfully Updated"
    Else
        lblMsg.Text = "Record is not Updated"
    End If
    con.Close()

And this is for Delete Code :

 Dim Query As String

    Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=test;User ID=root;Password=;")
    con.Open()
    Query = "Delete FROM userreg WHERE idUserReg  =" + txtUserRegId.Text

    Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
    MsgBox(Query)
    Dim i As Integer = cmd.ExecuteNonQuery()
    If (i > 0) Then
        lblMsg.Text = "Record is Successfully Deleted"
    Else
        lblMsg.Text = "Record is not Deleted"
    End If
    con.Close()

But when I want update and delete data, there is no change.

I'll help you out since I see from your other thread you're struggling.

This function will Update/Delete from the DB using parameterized queries:

Public Function ParameterizedNonQueryCommand(ByRef NonQuery As String, ByVal Parameters As List(Of MySqlParameter), _
        Optional ByVal Connection As MySql.Data.MySqlClient.MySqlConnection = Nothing) As Integer

        Dim comm As New MySql.Data.MySqlClient.MySqlCommand(NonQuery, Connection)
        Try
            For Each param As MySql.Data.MySqlClient.MySqlParameter In Parameters
                comm.Parameters.Add(param)
            Next
            If comm.Connection.State <> ConnectionState.Open Then comm.Connection.Open()
            comm.Prepare()
            Return comm.ExecuteNonQuery()
        Catch ex As Exception
            Return -1
        Finally
            comm.Connection.Close()
            comm.Dispose()
        End Try
    End Function

Example usage to delete the user with UserID 1 from a table:

Dim query as string = "DELETE FROM USERS WHERE USERID = ?UserID"
Dim Params As New List(Of MySqlParameter)
Params.Add(New MySqlParameter("UserID", 1))
Dim oConnection as MySqlConnection = New MySqlConnection("Data Source=localhost;Database=test;User ID=root;Password=;")
ParameterizedNonQueryCommand(query, Params, oConnection)

Basically, the function accepts the query as a string parameter. In your query string, where you need to add a value, just pass in a parameter placeholder with '?', then you specify the value to replace it with in a MySqlParameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM