Use MySQL database in. net

Source: Internet
Author: User

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

 

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.