Step 1: Download the MySQL driver package
Official is http://dev.mysql.com/downloads/connector/net
Step 2: Introduce Components
The downloaded file is a compressed file that is decompressed to a local disk. Find the mysql. Data. dll file in the bin folder. This is the component to be referenced. Reference it to your project through Visual Studio
DLL deployment process:
First put the DLL file under the application... \ bin \ debug;
Then add reference in solution: Right-click and choose add reference> browse> select DLL placement path and click OK ".
Note: Use the using MySQL. Data. mysqlclient command in the application file header.
Executereader () returns a typed datareader object. The returned object can be used to traverse the returned records.
Using system; using system. collections. generic; using system. LINQ; using system. text; using MySQL. data; using MySQL. data. mysqlclient; namespace mysqlconnectiontest {class program {static void main (string [] ARGs) {console. writeline ("--------"); string myconn = "database = 'test'; Data Source = localhost; user id = root; Password = 123456; charset = utf8 ;"; // The SQL statement to be executed string MySQL = "select * from users"; // create a database connection mysqlconnection myconnection = new mysqlconnection (myconn); myconnection. open (); // create the mysqlcommand object mysqlcommand mycommand = new mysqlcommand (MySQL, myconnection); // use the executereader () method of mysqlcommand to construct the datareader object mysqldatareader myreader = mycommand. executereader (); While (myreader. read () {console. writeline (myreader. getint32 (0) + "," + myreader. getstring (1) + "," + myreader. getstring (2);} myreader. close (); myconnection. close ();}}}
Executenonquery () is generally used for update, insert, or delete statements. The unique return value is the number of affected records.
Static void main (string [] ARGs) {string myconn = "database = 'test'; Data Source = localhost; user id = root; Password = 123456; charset = utf8 ;"; // create a database connection mysqlconnection dbconn = new mysqlconnection (myconn); dbconn. open (); // execute the query statement mysqlcommand dbcomm = new mysqlcommand ("Update users set user_passwd = '000000' where user_name = 'aaa'", dbconn); int rowsreturned = dbcomm. executenonquery (); // displays the data console. writeline ("{0} rows returned. ", rowsreturned); dbconn. close ();}
Static void main (string [] ARGs) {string myconn = "database = 'test'; Data Source = localhost; user id = root; Password = 123456; charset = utf8 ;"; // create a database connection mysqlconnection dbconn = new mysqlconnection (myconn); dbconn. open (); // execute the query statement mysqlcommand dbcomm = new mysqlcommand ("select count (*) from users", dbconn); // executescalar () returns a result from an SQL statement, such as the number of records in a given table, or the current date and time on the server. Object o = dbcomm. executescalar (); console. writeline (o );}