Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Diaplay Records in DataGridview from MySQL Database.

Display Records in
VB.Net from MySQL Database

Private Sub DisplayRecords_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button1.Click
Dim con As MySqlConnection = New MySqlConnection("Data
Source=localhost;Database=test;User ID=root;Password=mysql;")
Dim sql As MySqlCommand = New MySqlCommand("SELECT * FROM
userreg", con)

Dim ds As DataSet = New DataSet()

Dim DataAdapter1 As MySqlDataAdapter = New


MySqlDataAdapter()

con.Open()

DataAdapter1.SelectCommand = sql

DataAdapter1.Fill(ds, "Product")

DataGridView1.DataSource = ds

DataGridView1.DataMember = "Product"

con.Close()

End Sub

Insert Records in MySql Database.

Private Sub AddRecords_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles Button2.Click
Dim Query As String
'Query = "INSERT INTO userreg"
Dim con As MySqlConnection = New MySqlConnection("Data
Source=localhost;Database=test;User ID=root;Password=mysql;")
'Dim sql As MySqlCommand = New MySqlCommand(Query, con)

Query = "INSERT INTO userreg(idUserReg,UserName,


Age)VALUES("
Query = Query + txtUserRegId.Text + ",'" +
txtUserName.Text + "'," + txtAge.Text + ")"
con.Open()

Dim cmd As MySqlCommand = New MySqlCommand(Query, con)

Dim i As Integer = cmd.ExecuteNonQuery()


If (i > 0) Then
lblMsg.Text = "Record is Successfully Inserted"
Else
lblMsg.Text = "Record is not Inserted"
End If
con.Close()
End Sub

Update Records from MySQL Database.

Private Sub UpdateRecords_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button3.Click
Dim Query As String

Dim con As MySqlConnection = New MySqlConnection("Data


Source=localhost;Database=test;User ID=root;Password=mysql;")
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()
End Sub
Delete Records from MySQL Database

Private Sub DeleteRecords_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button4.Click
Dim Query As String

Dim con As MySqlConnection = New MySqlConnection("Data


Source=localhost;Database=test;User ID=root;Password=mysql;")
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()
End Sub
MySQL Database
Here I am creating table in MySQL Data base with Following Fields. The Table Name is
userreg.

idUserReg : int

UserName :
Varchar

Age : int

OutPut

OutPut

You might also like