How to connect to mysql DATA in c #

Source: Internet
Author: User
Tags mysql tutorial msdr

How to connect to mysql in c #
Method 1:
Mysql connector/net is an ado.net driver for mysql
This component is the. net access component designed by the mysql tutorial for accessing the mysql database from ado.net.
After the component is installed, reference the namespace mysql. data. mysqlclient;
When using command line compilation: csc/r: mysql. data. dll test. cs
Method 2:
Access the mysql database through odbc
Download two components: the odbc driver (mysql connector/odbc (myodbc) driver) of odbc.net and mysql. The current version is 3.51.
After the installation is complete, you can access the mysql database through odbc
Method 3:
Use the mysql access component released by corelab for. net
After the installation is complete, reference the namespace: corelab. mysql;
Run the command: csc/r: corelab. mysql. dll test. cs

Mysqlconnection conn = null;
Try
{
// Mysqlconnection conn = new mysqlconnection (new mysqlconnectionstring ("myquest", "root", "amttgroup"). asstring );
// Instantiate a connection object. myquest is the database name, root is the database username, and amttgroup is the database password.
String connstr = "data source = mysql; password = root; user id = root; location = localhost ";
Conn = new mysqlconnection (constr );
Conn. open ();
String query = "insert into test. dbtable values (10, 'disksidkfsdi ', 'asdfaf', 'adsfasdf ')";
String tmp = null;
Mysqlcommand cmd = new mysqlcommand (query, conn );
For (int I = 0; I <100000; I ++)
{
Cmd.exe cutenonquery ();
}
Cmd. dispose ();
Conn. close ();
Query = "select * from test. dbtable ";
Mysqlcommand cmd2 = new mysqlcommand (query, conn );
Conn. open ();

// Set Chinese output
Mysqlcommand firstcmd = new mysqlcommand ("set names gb2312", myconn );
Firstcmd.exe cutenonquery ();

Mysqldatareader reader = pai2.executereaderex ();
While (reader. read ())
{
Tmp = reader [0]. tostring ();
Tmp = reader [1]. tostring ();
Tmp = reader [2]. tostring ();
Tmp = reader [3]. tostring ();
}
Conn. close ();
Query = "delete from test. dbtable ";
Mysqlcommand cmd3 = new mysqlcommand (query, conn );
Conn. open ();
Statement 3.executenonquery ();
}
Catch (exception ex)
{
Messagebox. show (ex. message );
}
Finally
{
Conn. close ();
}


Mysqldrivercs enables access to the mysql database in the. net Environment

 

Using system;

Using system. data;

Using system. data. sqlclient;

Using system. configuration;

Using system. web;

Using system. web. security;

Using system. web. ui;

Using system. web. ui. webcontrols;

Using system. web. ui. webcontrols. webparts;

Using system.web.ui.html controls;

Using mysqldrivercs;

 

Public partial class _ default: system. web. ui. page

{

Protected void page_load (object sender, eventargs e)

{

Mysqlconnection conn = null;

Conn = new mysqlconnection (new mysqlconnectionstring ("127.0.0.1", "dvbbs", "root", "123456"). asstring );

Conn. open ();

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

Commn.exe cutenonquery ();

String SQL = "select title, topicid, boardid, dateandtime, hits from dv_topic order by dateandtime desc limit 10 ";

Mysqldataadapter mda = new mysqldataadapter (SQL, conn );

Dataset ds = new dataset ();

Mda. fill (ds, "table1 ");

This. repeater1.datasource = ds;

This. repeater1.databind ();

Conn. close ();

}

}

In this way, you can access mysql Data in the. net environment. Before that, you need to make some preparations. Download mysqldrivercs, install it, find mysqldriver. dll in the installation folder, and add and reference mysqldriver. dll to the project. Maybe you have code for this line

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

Commn.exe cutenonquery ();


Methods provided by netizens

 

Access the mysql database instance

Compilation command: csc/r: corelab. mysql. dll/r: mysql. data. dll test. cs

Using system;
Using system.net;
Using system. text;
Using corelab. mysql;
Using system. data. odbc;
Using mysql. data. mysqlclient;

Class connectmysql
{

Public void connect_corelab ()
{
String constr = "user id = root; host = localhost; database = qing; password = qing ";
Mysqlconnection mycn = new mysqlconnection (constr );
Mycn. open ();
Mysqlcommand mycm = new mysqlcommand ("select * from shop", mycn );
Mysqldatareader msdr = mycm.exe cutereader ();
While (msdr. read ())
{
If (msdr. hasrows)
{
Console. writeline (msdr. getstring (0 ));
}
}
Msdr. close ();
Mycn. close ();
}

Public void connect_odbc ()
{
// String myconstring = "dsn = mysql; uid = root; pwd = qing ";
String myconstring = "driver = {mysql odbc 3.51 driver};" +
"Server = localhost;" +
"Database = test;" +
"Uid = root;" +
"Password = qing;" +
"Option = 3 ";
Odbcconnection myconn = new odbcconnection (myconstring );
Myconn. open ();
Odbccommand mycm = new odbccommand ("select * from hello", myconn );
Odbcdatareader msdr = mycm.exe cutereader ();
While (msdr. read ())
{
If (msdr. hasrows)
{
Console. writeline (msdr. getstring (0 ));
}
}
Msdr. close ();
Myconn. close ();
}
Public void connect_net ()
{
String myconnectionstring = "database = test; data source = localhost; user id = root; password = qing ";
Mysqlconnection mycn = new mysqlconnection (myconnectionstring );
Mycn. open ();
Mysqlcommand mycm = new mysqlcommand ("select * from hello", mycn );
Mysqldatareader msdr = mycm.exe cutereader ();
While (msdr. read ())
{
If (msdr. hasrows)
{
Console. writeline (msdr. getstring (0 ));
}
}
Msdr. close ();
Mycn. close ();
}
Public static void main ()
{
Connectmysql MS = new connectmysql ();
Ms. connect_corelab ();
Ms. connect_odbc ();
Connect_net ();
}
}

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.