Using System.Data;
Using MySql.Data.MySqlClient;
Private mysqlconnection Conn;
Private DataTable data;
Private Mysqldataadapter da;
Private Mysqlcommandbuilder CB;
Private DataGrid DataGrid;
private void Connectbtn_click (object sender, System.EventArgs e)
{
if (conn! = null)
Conn. Close ();
String connstr = String.Format ("Server={0};user id={1}; PASSWORD={2}; PORT={3}; Database=mysql; Pooling=false; Charset=utf8 ",
Server. Text, UserID. Text, password. Text, 3306);
Try
{
conn = new mysqlconnection (CONNSTR);
Conn. Open ();
getdatabases ();
MessageBox.Show ("Connect database successfully!");
}
catch (Mysqlexception ex)
{
MessageBox.Show ("Error Connecting to the server:" + ex.) Message);
}
}
private void getdatabases ()
{
Mysqldatareader reader = null;
Mysqlcommand cmd = new Mysqlcommand ("SHOW DATABASES", conn);
Try
{
reader = cmd. ExecuteReader ();
databaseList.Items.Clear ();
while (reader. Read ())
{
databaseList.Items.Add (reader. GetString (0));
}
}
catch (Mysqlexception ex)
{
MessageBox.Show ("Failed to populate Database list:" + ex.) Message);
}
finally
{
if (reader! = null) reader. Close ();
}
}
private void Databaselist_selectedindexchanged (object sender, System.EventArgs e)
{
Mysqldatareader reader = null;
Conn. ChangeDatabase (DatabaseList.SelectedItem.ToString ());
Mysqlcommand cmd = new Mysqlcommand ("SHOW TABLES", conn);
Try
{
reader = cmd. ExecuteReader ();
tables. Items.clear ();
while (reader. Read ())
{
tables. Items.Add (reader. GetString (0));
}
}
catch (Mysqlexception ex)
{
MessageBox.Show ("Failed to populate Table list:" + ex.) Message);
}
finally
{
if (reader! = null) reader. Close ();
}
}
private void Tables_selectedindexchanged (object sender, System.EventArgs e)
{
data = new DataTable ();
da = new Mysqldataadapter ("SELECT * from" + tables. Selecteditem.tostring (), conn);
cb = new Mysqlcommandbuilder (DA); Must be there, otherwise it cannot be updated
da. Fill (data);
datagrid.datasource = data;
}
private void Updatebtn_click (object sender, System.EventArgs e)
{
DataTable changes = data. GetChanges ();
da. Update (changes);
data. AcceptChanges ();
}