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 ();