C # connect to MySQL Database

Source: Internet
Author: User

Mysql.data.dll download _c# connection mysql necessary plugin

Mysql.data.dll is C # operation MySQL driver file, is C # connection MySQL necessary plugin, make C # language more concise operation MySQL database. When your computer pops up with a "lost Mysql.data.dll" or "Can't find Mysql.data.dll" error, please download the DLL file provided by this website and use it to help users solve the above problems.
DLL File Repair method:
1, unzip the downloaded file.
2, copy the file "Mysql.data.dll" to the system directory.
3, the system directory is generally: C:\WINNT\System32 64-bit system for C:\Windows\SysWOW64
4. Finally click Start Menu--run--Enter regsvr32 mysql.data.dll, enter to resolve the error prompt!

Third-party components: Mysql.Data.dll
Description: Download Mysql.Data.dll, then add a reference to the component in the project, enter using Mysql.Data.MysqlClient in the code page, and we will be able to make a connection using the class library's function successfully.

Here are a few common functions:
#regionEstablishing a MySQL database connection/// <summary>    ///establish a database connection. /// </summary>    /// <returns>returns the Mysqlconnection object</returns>     Publicmysqlconnection Getmysqlcon () {//http://sosoft.cnblogs.com/        stringM_str_sqlcon ="Server=localhost;user id=root;password=root;database=abc";//according to your own settingsMysqlconnection Mycon =Newmysqlconnection (M_str_sqlcon); returnMycon; }    #endregion    #regionExecute Mysqlcommand command/// <summary>    ///Executive Mysqlcommand/// </summary>    /// <param name= "M_str_sqlstr" >SQL statements</param>     Public voidGetmysqlcom (stringm_str_sqlstr) {mysqlconnection Mysqlcon= This. Getmysqlcon (); Mysqlcon.        Open (); Mysqlcommand mysqlcom=NewMysqlcommand (M_str_sqlstr, Mysqlcon); Mysqlcom.        ExecuteNonQuery (); Mysqlcom.        Dispose (); Mysqlcon.        Close (); Mysqlcon.    Dispose (); }    #endregion#regionCreate a Mysqldatareader object/// <summary>    ///Create a Mysqldatareader object/// </summary>    /// <param name= "M_str_sqlstr" >SQL statements</param>    /// <returns>returns the Mysqldatareader object</returns>     PublicMysqldatareader Getmysqlread (stringm_str_sqlstr) {mysqlconnection Mysqlcon= This. Getmysqlcon (); Mysqlcommand mysqlcom=NewMysqlcommand (M_str_sqlstr, Mysqlcon); Mysqlcon.        Open (); Mysqldatareader Mysqlread=mysqlcom.        ExecuteReader (commandbehavior.closeconnection); returnMysqlread; }    #endregion
usingSystem.Data;usingMySql.Data.MySqlClient;PrivateMysqlconnection Conn;PrivateDataTable data;PrivateMysqldataadapter da;PrivateMysqlcommandbuilder CB;Privatedatagrid DataGrid;Private voidConnectbtn_click (Objectsender, System.EventArgs e) { if(Conn! =NULL) Conn.  Close (); stringConnStr = 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=Newmysqlconnection (CONNSTR); Conn. Open ();                Getdatabases (); MessageBox.Show ("Connect to the database successfully!"); } Catch(Mysqlexception ex) {MessageBox.Show ("Error connecting to the server:"+Ex. Message); } } Private voidgetdatabases () {Mysqldatareader reader=NULL; Mysqlcommand cmd=NewMysqlcommand ("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 voidDatabaselist_selectedindexchanged (Objectsender, System.EventArgs e) {Mysqldatareader Reader=NULL; Conn. ChangeDatabase (DatabaseList.SelectedItem.ToString ());//http://sosoft.cnblogs.com/Mysqlcommand cmd=NewMysqlcommand ("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 voidTables_selectedindexchanged (Objectsender, System.EventArgs e) {Data=NewDataTable (); Da=NewMysqldataadapter ("SELECT * from"+tables.     Selecteditem.tostring (), conn); CB=NewMysqlcommandbuilder (DA);//must be there, otherwise it cannot be updatedda. Fill (data); Datagrid.datasource=data;} Private voidUpdatebtn_click (Objectsender, System.EventArgs e) {DataTable Changes=data. GetChanges (); Da. Update (changes); Data. AcceptChanges (); }

C # connect to MySQL Database

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.