There are three methods for connecting DOTNET to the MySQL database:
Method 1:
MySQL connector/net is an ADO. Net driver for MySQL
This component is a. Net access component designed for accessing the MySQL database by using 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
Before accessing, you must download two components: the ODBC driver for odbc.net and MySQL (MySQL connector/ODBC (myodbc) Driver) is currently version 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
Access the MySQL database instance
Compilation command: CSC/R: CoreLab. MySQL. dll/R: mysql. Data. dll test. CSUsingSystem;
UsingSystem. net;
UsingSystem. text;
UsingCoreLab. MySQL;
UsingSystem. Data. ODBC;
UsingMySQL. 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. executereader ();
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. executereader ();
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. executereader ();
While (Msdr. Read ())
{
If (Msdr. hasrows)
{
Console. writeline (msdr. getstring ( 0 ));
}
}
Msdr. Close ();
Mycn. Close ();
}
Public Static VoidMain ()
{
Connectmysql MS= NewConnectmysql ();
Ms. connect_corelab ();
Ms. connect_odbc ();
Connect_net ();
}
}
Appendix:You can use oradirect.net data provider of CoreLab to connect to and access the Oracle database. However, the oradirect.net data provider connection component of CoreLab is not free of charge. The downloaded demo version can only read the first eight columns.
Source: http://school.ogdev.net/ArticleShow.asp? Id = 3550 & categoryid = 19