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

All download: http://hovertree.com/h/bjaf/0sft36s9.htm

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:

1 # region create a MySql database connection 2 // <summary> 3 // create a database connection. 4 /// </summary> 5 // <returns> return MySqlConnection object </returns> 6 public MySqlConnection getmysqlcon () 7 {8 // http://sosoft.cnblogs.com/ 9 string M_str_sqlcon = "server = localhost; user id = root; password = root; database = abc"; // set 10 MySqlConnection myCon = new MySqlConnection (M_str_sqlcon) according to your own settings ); 11 return myCon; 12} 13 # endregion14 15 # region execute the MySqlCommand command 16 // <summary> 17 // execute MySqlCommand18 /// </summary> 19 // <param name =" m_str_sqlstr "> SQL statement </param> 20 public void getmysqlcom (string M_str_sqlstr) 21 {22 MySqlConnection mysqlcon = this. getmysqlcon (); 23 mysqlcon. open (); 24 MySqlCommand mysqlcom = new MySqlCommand (M_str_sqlstr, mysqlcon); 25 mysqlcom. executeNonQuery (); 26 mysqlcom. dispose (); 27 mysqlcon. close (); 28 mysqlcon. dispose (); 29} 30 # endregion31 32 # region create MySqlDataReader object 33 // <summary> 34 // create a MySqlDataReader object 35 /// </summary> 36 // <param name = "M_str_sqlstr"> SQL statement </param> 37 // <returns> returns the MySqlDataReader object </returns> 38 public MySqlDataReader getmysqlread (string M_str_sqlstr) 39 {40 MySqlConnection mysqlcon = this. getmysqlcon (); 41 MySqlCommand mysqlcom = new MySqlCommand (M_str_sqlstr, mysqlcon); 42 mysqlcon. open (); 43 MySqlDataReader mysqlread = mysqlcom. executeReader (CommandBehavior. closeConnection); 44 return mysqlread; 45} 46 # endregion

 

 

 

 

1 using System. data; 2 3 using MySql. data. mySqlClient; 4 5 6 7 8 9 private MySqlConnection conn; 10 11 private DataTable data; 12 13 private MySqlDataAdapter da; 14 15 private MySqlCommandBuilder cb; 16 17 private DataGrid dataGrid; 18 19 20 21 private void connectBtn_Click (object sender, System. eventArgs e) 22 23 {24 25 if (conn! = Null) 26 27 conn. close (); 28 29 30 31 string connStr = String. format ("server = {0}; user id = {1}; password = {2}; port = {3}; database = mysql; pooling = false; charset = utf8 ", 32 33 server. text, userid. text, password. text, 3306); 34 35 36 37 try 38 39 {40 41 conn = new MySqlConnection (connStr); 42 43 conn. open (); 44 45 46 47 GetDatabases (); 48 49 MessageBox. show ("database connected! "); 50 51} 52 53 catch (MySqlException ex) 54 55 {56 57 MessageBox. show ("Error connecting to the server:" + ex. message); 58 59} 60 61} 62 63 64 65 private void GetDatabases () 66 67 {68 69 MySqlDataReader reader = null; 70 71 MySqlCommand cmd = new MySqlCommand ("show databases", conn); 72 73 74 75 try 76 77 {78 79 reader = cmd. executeReader (); 80 81 databaseList. items. clear (); 82 83 while (Reader. read () 84 85 {86 87 databaseList. items. add (reader. getString (0); 88 89} 90 91} 92 93 catch (MySqlException ex) 94 95 {96 97 MessageBox. show ("Failed to populate database list:" + ex. message); 98 99} 100 101 finally 102 103 {104 105 if (reader! = Null) reader. close (); 106 107} 108 109} 110 111 112 113 private void databaseList_SelectedIndexChanged (object sender, System. eventArgs e) 114 115 {116 117 MySqlDataReader reader = null; 118 119 conn. changeDatabase (databaseList. selectedItem. toString (); 120 121 // http://sosoft.cnblogs.com/122 123 MySqlCommand cmd = new MySqlCommand ("show tables", conn); 124 125 try 126 127 128 {129 reader = cmd. executeReader (); 130 131 tables. items. clear (); 132 133 while (reader. read () 134 135 {136 137 tables. items. add (reader. getString (0); 138 139} 140 141} 142 143 catch (MySqlException ex) 144 145 {146 147 MessageBox. show ("Failed to populate table list:" + ex. message); 148 149} 150 151 finally 152 153 {154 155 if (reader! = Null) reader. close (); 156 157} 158 159} 160 161 162 163 private void tables_SelectedIndexChanged (object sender, System. eventArgs e) 164 165 {166 167 data = new DataTable (); 168 169 170 171 da = new MySqlDataAdapter ("SELECT * FROM" + tables. selectedItem. toString (), conn); 172 173 cb = new MySqlCommandBuilder (da); // This is required; otherwise, the 174 175 176 177 da cannot be updated. fill (data); 178 179 180 181 dataGrid. dataSource = data; 182 183} 184 185 186 187 private void updateBtn_Click (object sender, System. eventArgs e) 188 189 {190 191 DataTable changes = data. getChanges (); 192 193 da. update (changes); 194 195 data. acceptChanges (); 196 197}

Http://www.cnblogs.com/sosoft/p/kaifajishu.html

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.