VB.Net Source Code: Save, Update, and Delete Data from MySQL

The following codes below is for saving, updating, and deleting data from MySQL database using VB.Net. The code below is tested on VB.Net 2008.

Before inserting the save, update, and delete code to your project,  you must add the MySQL imports so that you cannot encounter any error. If you don’t know how to do it, please visit Source Code: VB.Net to MySQL Connection.


Save Data

Str = "INSERT INTO tablename VALUES('" & textbox1.Text & "','" & textbox2.Text & "')"
conn.Open()
Dim mysc As New MySqlCommand(Str, conn)
mysc.ExecuteNonQuery()
MessageBox.Show("Data Added!", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information)
conn.Close()

Update Data

Str = "UPDATE tablename SET column1='" & textbox1.Text & "',column2='" & textbox2.Text & "' WHERE id='"
& textboxid.Text & "'"
conn.Open()
Dim mysc As New MySqlCommand(Str, conn)
mysc.ExecuteNonQuery()
MessageBox.Show("Data Updated!", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information)
conn.Close()

Delete Data

If (MessageBox.Show("Are you sure you want to delete this data?", "Confirmation", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes) Then
    Str = "DELETE FROM tablename WHERE id='" & textboxid.Text & "'"
    conn.Open()
    Dim mysc As New MySqlCommand(Str, conn)
    mysc.ExecuteNonQuery()
    MessageBox.Show("Data Deleted!", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information)
    conn.Close()
End If

 

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz