How to connect to the ODBC data source in. net

Source: Internet
Author: User
Tags dsn

1. Download ODBC. Net (Framework 2.0 and above are not installed by default)

:Http://www.microsoft.com/downloads/details.aspx? Familyid = 6ccd8427-1017-4f33-a062-d165078e32b1

2. Create a project and addMicrosoft. Data. ODBC. dllReference

3. CSCodeAs follows:

Using System. Data;
Using Microsoft. Data. ODBC;

4. Sample connection code:

Sqlserver:

Odbcconnection CN;
Odbccommand cmd;
String Mystring;

Mystring = " Select * from MERs " ;

CN =   New Odbcconnection ( " Driver = {SQL Server}; server = mysqlserver; uid = sa;
PWD = Mypassword; Database = Northwind; " );

CMD = New Odbccommand (mystring, CN );
CN. open ();

MessageBox. Show ( " Connected " );

CN. Close ();

 

Oledb jet:

Odbcconnection CN;
Odbccommand cmd;
String Mystring;

Mystring = " Select * from titles " ;

CN =   New Odbcconnection ( " Driver = {Microsoft Access Driver (*. mdb )};
DBQ = D: \ Program Files \ Microsoft Office \ office10 \ samples \ northwind. mdb; uid = ; Pwd = ; " );

CMD = New Odbccommand (mystring, CN );
CN. open ();
MessageBox. Show ( " Connected " );

CN. Close ();

ORACLE:

 

Odbcconnection CN;
Odbccommand cmd;
String Mystring;

Mystring = " Select * from MERs " ;

CN =   New Odbcconnection ( " Driver = {Microsoft ODBC for Oracle}; server = myoracleserver;
UID = Demo; pwd = Demo; " );

CMD = New Odbccommand (mystring, CN );
CN. open ();

MessageBox. Show ( " Connected " );

CN. Close ();

DSN:

 

Odbcconnection CN;
Odbccommand cmd;
String Mystring;

Mystring = " Select * from MERs " ;

CN =   New Odbcconnection ( " DSN = mydsn; uid = myuid; Pwd = mypwd; " );

CMD = New Odbccommand (mystring, CN );

CN. open ();
MessageBox. Show ( " Connected " );

CN. 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.