C # how to connect to the MySQL database

Source: Internet
Author: User

 Use mysqldrivercs to connect to the MySQL database

Download and install mysqldrivercs first. Address:

Http://sourceforge.net/projects/mysqldrivercs/

Find mysqldriver. dll in the installation folder, and add the mysqldriver. DLL to the project.

Note: the version I downloaded is a MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe

Using system;

Using system. Collections. Generic;

Using system. componentmodel;

Using system. Data;

Using system. Data. ODBC;

Using system. drawing;

Using system. LINQ;

Using system. text;

Using system. Windows. forms;

Using mysqldrivercs;

Namespace MySQL

{

Public partial class form1: Form

{

Public form1 ()

{

Initializecomponent ();

}

 

Private void form1_load (Object sender, eventargs E)

{

 

Mysqlconnection conn = NULL;

Conn = new mysqlconnection (New mysqlconnectionstring ("localhost", "CRM", "root", "QAZ"). asstring );

Conn. open ();

// The following two lines are very important. Otherwise, Chinese characters are garbled.

Mysqlcommand commn = new mysqlcommand ("set names gb2312", Conn );

Commn. executenonquery ();

 

String SQL = "select * from exchange ";

Mysqldataadapter MDA = new mysqldataadapter (SQL, Conn );

 

Dataset DS = new dataset ();

MDA. Fill (DS, "Table1 ");

 

Datagridview1.datasource = Ds. Tables ["Table1"];

Conn. Close ();

}

}

}

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.