VB.NET Tutorial: Save, Update, and Delete information from MySQL Database

Saving, Updating and Deleting  information from MySQL database are the basic functionality of web and desktop application program.

Below are the code on how to save, update, and delete data from MySQL with VB.NET. In this tutorial I am using VB.NET 2008 and it is also applicable to other version.

Code for saving data

Str = “INSERT INTO studentinfo VALUES(‘” & txtidnumber.Text & “‘,'” & txtfname.Text & “‘)”
conn.Open()           
Dim mysc As New MySqlCommand(Str, conn)
mysc.ExecuteNonQuery()
MsgBox(“Student Successfully Added!”, MsgBoxStyle.Information)
conn.Close()

Code for updating data

Str = “UPDATE studentinfo SET idnumber='” & txtidnumber.Text & “‘,fname='” & txtfname.Text & “‘ WHERE id='” & txtid.text & “‘”
conn.Open()           
Dim mysc As New MySqlCommand(Str, conn)
mysc.ExecuteNonQuery()
MsgBox(“Student Successfully Updated!”, MsgBoxStyle.Information)
conn.Close()

Code for deleting data

If (MsgBox(“Are your sure you want to delete this student?”, MsgBoxStyle.YesNo) = MsgBoxResult.Yes) Then
                Str = “delete from studentinfo where idnumber='” & txtidnumber.Text & “‘”
                conn.Open()
                Dim mysc As New MySqlCommand(Str, conn)
                mysc.ExecuteNonQuery()
                MsgBox(“Student Successfully deleted!”, MsgBoxStyle.Information)
                conn.Close()
 End If

If you have encountered error on the following code above, feel free to add your comment below.

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz