C # connect to the Mysql database,

Source: Internet
Author: User

C # connect to the Mysql database,

Mysql. data. dll download _ c # necessary plug-ins for connecting to mysql

Mysql. data. dll is a C # MYSQL driver file. It is a necessary plug-in for c # To connect to mysql, so that c # can operate mysql databases more concisely. When your computer prompts "mysql is lost. data. dll or mysql cannot be found. data. dll "and other errors, please download the dll file provided for you on this site, use it to help users solve the above problems.
Dll file repair method:
1. decompress the downloaded file.
2. copy the file "mysql. data. dll" to the system directory.
3. The system directory is C: \ WINNT \ System32 64-bit. The system directory is C: \ Windows \ SysWOW64.
4. Click the Start menu --> Run --> enter regsvr32 mysql. data. dll and press enter to solve the error!

Third-party components: Mysql. Data. dll
Download Mysql. data. dll, and then add reference to this component in the project. In the code page, enter using Mysql. data. mysqlClient, We can smoothly use the function of this class library to establish a connection.

The following are some common functions:
# Region create a MySql database connection // <summary> // create a database connection. /// </summary> /// <returns> returns the MySqlConnection object </returns> public MySqlConnection getmysqlcon () {// http://sosoft.cnblogs.com/string M_str_sqlcon = "server = localhost; user id = root; password = root; database = abc "; // set MySqlConnection myCon = new MySqlConnection (M_str_sqlcon); return myCon ;} # endregion # region execute the MySqlCommand command /// <summary> /// execute MySqlCommand /// </summary> /// <param name = "M_str_sqlstr"> SQL statement </ param> public void getmysqlcom (string M_str_sqlstr) {MySqlConnection mysqlcon = this. getmysqlcon (); mysqlcon. open (); MySqlCommand mysqlcom = new MySqlCommand (M_str_sqlstr, mysqlcon); mysqlcom. executeNonQuery (); mysqlcom. dispose (); mysqlcon. close (); mysqlcon. dispose ();} # endregion # region create MySqlDataReader object // <summary> // create a MySqlDataReader object // </summary> // <param name = "M_str_sqlstr"> SQL statement </param> // <returns> returns the MySqlDataReader object </returns> public MySqlDataReader getmysqlread (string M_str_sqlstr) {MySqlConnection mysqlcon = this. getmysqlcon (); MySqlCommand mysqlcom = new MySqlCommand (M_str_sqlstr, mysqlcon); mysqlcon. open (); MySqlDataReader mysqlread = mysqlcom. executeReader (CommandBehavior. closeConnection); return mysqlread;} # endregion
Using System. data; using MySql. data. mySqlClient; private MySqlConnection conn; private DataTable data; private MySqlDataAdapter da; private MySqlCommandBuilder cb; private 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 ("database connected! ");} 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) {mestion EBox. 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 (); // http://sosoft.cnblogs.com/MySqlCommand cmd = new MySqlCommand ("show tables", conn); try {reader = cmd. executeReader (); tables. items. clear (); while (reader. read () {tables. items. add (reader. getStr Ing (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); // This is required; otherwise, da cannot be updated. 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.