How to connect database in Ado.net

Source: Internet
Author: User
Tags connect odbc mssql sql net ole oracleconnection string
ado| Data | database | database connection
In MSDN, the. NET database connection strings are described in detail, and I'll list them here in code example, and the meaning of each representative can be reviewed in MSDN.

Database connection mode in Ado.net (provided by Microsoft)

Microsoft offers the following four ways to connect to a database:
System.Data.OleDb.OleDbConnection
System.Data.SqlClient.SqlConnection
System.Data.Odbc.OdbcConnection
System.Data.OracleClient.OracleConnection
Here's an example of how to:

System.Data.SqlClient.SqlConnection
Some common connection strings (C # code):

SqlConnection Conn
= New SqlConnection ("server= (local); integrated security=sspi;database=pubs");

SqlConnection Conn
= New SqlConnection ("server= (local) \\netsdk;database=pubs;integrated Security=sspi");

SqlConnection conn = new SqlConnection (
"Data source=localhost;integrated security=sspi;initial catalog=northwind;");

SqlConnection conn = new SqlConnection (
"Data source= (local); initial catalog=xr;integrated Security=sspi;
Persist Security Info=false;workstation Id=xurui;packet size=4096; ");

SqlConnection myconn = new
System.Data.SqlClient.SqlConnection ("Persist Security info=false;integrated
Security=sspi;database=northwind;server=mysqlserver ");

SqlConnection conn = new SqlConnection (
"Uid=sa;pwd=passwords;initial Catalog=pubs;data source=127.0.0.1; Connect timeout=900 ");

More string connection instructions see MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/ Frlrfsystemdatasqlclientsqlconnectionclassconnectionstringtopic.asp

System.Data.OleDb.OleDbConnection
Some common connection strings (C # code):

OleDbConnection conn = new OleDbConnection (@ "Provider=Microsoft.Jet.OLEDB.4.0;Data source=d:\myweb\81\05\ Grocertogo.mdb ");

OleDbConnection conn = new OleDbConnection (
@ "Provider=Microsoft.Jet.OLEDB.4.0; password=;
User id=admin;data source=grocertogo.mdb; ");

OleDbConnection conn = new OleDbConnection (
"Provider=msdaora; Data Source=oracle8i7; Persist security info=false;integrated Security=yes ");

OleDbConnection conn = new OleDbConnection (
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\bin\localaccess40.mdb ");

OleDbConnection conn = new OleDbConnection (
"Provider=sqloledb;data source=mysqlserver;integrated Security=sspi");

More string connection instructions see MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/ Frlrfsystemdataoledboledbconnectionclassconnectionstringtopic.asp?frame=true


System.Data.OracleClient.OracleConnection
Some common connection strings (C # code):

OracleConnection myconn = new System.Data.OracleClient.OracleConnection (
"Data source=oracle8i;integrated security=yes");



More string connection instructions see MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/ Frlrfsystemdataoracleclientoracleconnectionclassconnectionstringtopic.asp?frame=true


System.Data.Odbc.OdbcConnection
Some common connection strings (C # code):


OdbcConnection conn = new OdbcConnection (
"Driver={sql Server}; Server=myserver; Trusted_connection=yes;database=northwind; ");

OdbcConnection conn = new OdbcConnection (
"Driver={microsoft ODBC for Oracle}; Server=oracle8i7;
Persist Security Info=false; Trusted_connection=yes ");

OdbcConnection conn = new OdbcConnection (
"Driver={microsoft Access Driver (*.mdb)};D Bq=c:\bin\nwind.mdb");

OdbcConnection conn = new OdbcConnection (
"Driver={microsoft Excel Driver (*.xls)};D Bq=c:\bin\book1.xls");


OdbcConnection conn = new OdbcConnection (
"Driver={microsoft Text Driver (*.txt; *.csv)};D bq=c:\bin ");

OdbcConnection conn = new OdbcConnection ("Dsn=dsnname");

More string connection instructions see MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/ Frlrfsystemdataodbcodbcconnectionclassconnectionstringtopic.asp?frame=true


database connections provided by other vendors:

Db2connection myconn = new IBM. Data.DB2.DB2Connection (
"DATABASE = SAMPLE; uid=<username>; pwd=<password>; ");

Db2connection myconn = new IBM. Data.DB2.DB2Connection ("DATABASE = SAMPLE");


Bdpconnection myconn = new Borland.Data.Provider.BdpConnection ("Assembly=borl
and. Data.mssql,version=1.1.0.0,culture=neutral,publickeytoken=91d62ebb5b0d1b1b;ve
Ndorclient=sqloledb.dll;osauthentication=false;database=<database>;usernam
e=<user>;hostname=
Bdpconnection myconn = new Borland.Data.Provider.BdpConnection ("Assembly=borl
and. Data.db2,version=1.1.0.0,culture=neutral,publickeytoken=91d62ebb5b0d1b1b;ve
ndorclient=db2cli.dll;database=<database>;username=<user>;
password=<password>;p rovider=db2 ");


Connection pooling


Implicit connection pool connection support is provided in the data provider in SQL Server, OLE DB, and the. NET Framework structure. You can specify different parameter values in the ConnectionString to control the behavior of the connection pool. For example, the following example causes the connection pool of OLE DB to be invalid and automatically transacted:
Provider=sqloledb;ole DB services=-4;data source=localhost;integrated Security=sspi;
The following parameter settings are provided in SQL Server.NET Data provider to control the behavior of the connection pool: Connection lifttime, Connection Reset, Enlist, Max pool Size, Min pool Size and pooling.

More database connection information, as well as a ado.net connection string, can be used to refer to:
http://www.connectionstrings.com/
--Reprint


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.