C # connect to MySQL Database

Source: Internet
Author: User

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 ();

}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.