Connect to the MySQL database in C #

Source: Internet
Author: User

This article will connect to the MySQL database in two ways. There may be many connection methods, but here we only use two of them:
One is written by a third party: bytefx, and the other is completed through odbcconnection.

First, make sure that your machine has installed MySQL and runs it correctly.

Bytefx. Data. mysqlclient;

...
...
...


 

Myconnectionstring = "DSN = MySQL; uid = root; Pwd = root ";
Mysqlconnection myconnection = new mysqlconnection (myconnectionstring );
String myinsertquery = "select * from authors ";
Mysqlcommand mycommand = new mysqlcommand (myinsertquery );
Mycommand. Connection = myconnection;
Myconnection. open ();
Mysqldatareader objreader = mycommand. executereader ();
Int nresultcount = 0;
While (objreader. Read ())
{
++ Nresultcount;
}

Another method is to use ODBC:

Using system. Data. ODBC;
...
...

String myconnectionstring = "DSN = MySQL; uid = root; Pwd = root ";
Odbcconnection myconn;
Odbccommand mycmd = new odbccommand ();
Myconn = new odbcconnection (myconnectionstring );
Myconn. open ();
Mycmd. Connection = myconn;
Stringbuilder SQL = new stringbuilder ();
SQL. append ("select ");
SQL. append ("*");
SQL. append ("from ");
SQL. append ("Authors ");
Mycmd. commandtext = SQL. tostring ();
Odbcdatareader result = mycmd. executereader (commandbehavior. closeconnection );
Int nresultcount = 0;
While (result. Read ())
{
++ Nresultcount;
}

 

 
 

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.