In this article I will explain with an example, how to use MySqlDataAdapter in Windows Forms (WinForms) Application using C# and VB.Net.
For illustration purposes, this article will use DataGridView to display the data populated from database using MySqlDataAdapter in C# and VB.Net.
What is MySqlDataAdapter?
The
MySqlDataAdapter works as a bridge between a DataSet or DataTable and
MySQL Database for retrieving and saving data.
The MySqlDataAdapter uses disconnection-oriented architecture i.e. it does not requires an open connection to the Data Source while retrieving the data.
It is used to fill the DataSet or DataTable and update the data source as well.
It has a Fill method, which adds or refreshes rows in the DataSet or DataTable to match those in the Data Source.
The MySqlDataAdapter class has following constructors.
1. MySqlDataAdapter (): Initializes a new instance of the MySqlDataAdapter class.
2. MySqlDataAdapter (MySqlCommand): Initializes a new instance of the MySqlDataAdapter class with the specified MySqlCommand as the SelectCommand property.
3. MySqlDataAdapter (String, MySqlConnection): Initializes a new instance of the MySqlDataAdapter class with a SelectCommand and a MySqlConnection object.
4. MySqlDataAdapter (String, String): Initializes a new instance of the MySqlDataAdapter class with the SelectCommand and a connection string.
Note: For more details about
MySqlDataAdapter, please refer
MSDN.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Form Design
The Form consists of following control:
DataGridView – For displaying data.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient
Populating DataTable using MySqlDataAdapter in C# and VB.Net
Inside the Form Load event handler, first the connection string is read from App.Config file.
Then, the MySqlDataAdapter object is initialized with the MySqlCommand and using the Fill function, the DataTable is populated with the records from database.
Finally, the DataTable is assigned to the DataSource property of DataGridView and DataGridView is populated.
C#
private void Form1_Load(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sql = "SELECT CustomerId, Name, Country FROM Customers";
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter(sql, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim sql As String = "SELECT CustomerId, Name, Country FROM Customers"
Using con As MySqlConnection = New MySqlConnection(constr)
Using sda As MySqlDataAdapter = New MySqlDataAdapter(sql, con)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
dataGridView1.DataSource = dt
End Using
End Using
End Using
End Sub
Screenshot
Downloads