Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! All
I am student of C# beginner. I want to load mysql table to datagridview of C#. Please someone help me ! Thanks you all.
Posted
Updated 19-Oct-12 6:39am
v2

Note-if you want more about c# and mysql go to youtube.com and search for "programming knowledge" channel.
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Threading;

namespace FINALIST
{
public partial class Purchase : Form
{
public Purchase()
{
InitializeComponent();
load_table();
}


void load_table()
{
string constring = "datasource=localhost;port=3306;username=root;password=triple333";
MySqlConnection conDataBase = new MySqlConnection(constring);

MySqlCommand cmdDataBase = new MySqlCommand(" select date as 'Date',voucherNo as 'Voucher No',vendorName as 'Vendor Name',product as 'Product' from test.purchase ;", conDataBase);

//date, voucherNo, vendorName and product are variables of purchase table of test database.
try
{

MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();

bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);

}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}

}


}
}
 
Share this answer
 
Hi
To communicate with mysql you need to add mysql.data.dll in your project
You can download this dll from Here

after that add the namespace
C#
using MySql.Data.MySqlClient;

MySqlConnection conn = new MySqlConnection(connectionstring);
MySqlCommand cmd = new MySqlCommand("Your sql query");
MySqlDataReader dr=cmd.ExecuteReader();

gridview1.datasource=dr;
gridview1.databind();


your connection string will be like this
<connectionstrings>
		<add name="connectionstring" connectionstring="Data source=server;database=mydb;user id=root;password=1234" providername="MySql.Data.MySqlClient" />
	</connectionstrings>


If you need documentation Click Here

I hope this will help you....
 
Share this answer
 
v2
 
Share this answer
 
1) Create a connection to your database.
2) Query for the data you want to show.
3) Bind that data to a datagridview control that you have placed on your form.
4) When something code related in this process doesn't work, ask that specific question here so we can help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900