DOTNET uses CoreLab's MySQL access component example (learning)

Source: Internet
Author: User
Tags dotnet msdr

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

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.