MySQL Connection and DataGridView in VB.NET
Requirements:
MySql Connector/Net
Download: Here
First of before starting VB.NET you should have downloaded and installed MySql Connector.
Now in your project, add the connector to your refrence:
And scroll down till you find "MySql.Data" then press Add
Now you have added the refrence..
Now go into your code, and add the following:
Code:
Imports MySql.Data.MySqlClient
And under Public Class Form1 add:
Code:
Dim conn As Common.DbConnection
Dim da As Common.DbDataAdapter
Dim ds As DataSet = New DataSet
Dim cnString As String
Dim sqlQRY As String
It should now look like this:
Now to your design:
You should make it look like this on the form:
It is simpley just the following:
5x TextBox
2x Button
5 x Labels
1 x GroupBox
and 1 x DataGridView
Everyone of them are default in VB.NET
Now when you have inserted it in your way, then you should double click the button where it says "Get Data", for editing your code for Button1_Click
At this point, you should insert the following under Button1_Click:
Code:
cnString = "datasource=" + TextBox1.Text + ";username=" + TextBox4.Text + ";password=" + TextBox5.Text + ";database=" + TextBox2.Text + ""
sqlQRY = "Select * from " + TextBox3.Text + ""
conn = New MySqlConnection(cnString)
Try
conn.Open()
da = New MySqlDataAdapter(sqlQRY, conn)
Dim cb As MySqlCommandBuilder = New MySqlCommandBuilder(da)
da.Fill(ds, "" + TextBox3.Text + "")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "" + TextBox3.Text + ""
Catch ex As Common.DbException
MsgBox(ex.ToString)
Finally
conn.Close()
End Try
And it will look like this:
Now you have the connection and fetching done, and when you open you application it will now load every data in the table of yours.
Now you will have to make the button that says "Save Data" work, so when you press it, it will save the data you edited in the fields:
As before, double click the button and it will go to its code.
Insert the following:
Code:
da.Update(ds, "" + TextBox3.Text + "")
MsgBox("Data sent", MsgBoxStyle.OkOnly, "Sucess")
It will look like this:
And now you just made a application fetching data from your SQL database, and you are also able to edit it!
Hope it helped you out!
If you have any problems with it, I attached my source down below!
[ATTACH]3767[/ATTACH
Cheerio!
Attached Files
Why fill up a signature?
Post Thanks / Like - 1 Thanks
NovaYear (1 members gave Thanks to EmiloZ for this useful post)
Banned
Looks nice, but I prefer the ADO.NET connector, which allows you to use any db, mySQL, Oracle, MSSQL, etc. No big deal but ideal for larger projects and projects that require high flexability.
Looks good! Would love to see a ADO.NET method also. Would save me a little time in the future.
Private
thx emiloz !! I could not download !! this project !!
Private
esta vació el contenido, no carga nada