ADO.net 中数据库连接方式
2024-07-10 13:03:31
供稿:网友
在msdn中,.net的数据库连接字符串都有详细的说明,我这里以代码范例的方式罗列一些,具体的每一项代表的意义可以参看msdn.
ado.net 中数据库连接方式(微软提供)
微软提供了以下四种数据库连接方式:
system.data.oledb.oledbconnection
system.data.sqlclient.sqlconnection
system.data.odbc.odbcconnection
system.data.oracleclient.oracleconnection
下面我们以范例的方式,来依次说明:
system.data.sqlclient.sqlconnection
常用的一些连接字符串(c#代码):
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");
更多字符串连接说明请看msdn:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclientsqlconnectionclassconnectionstringtopic.asp
system.data.oledb.oledbconnection
常用的一些连接字符串(c#代码):
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");
更多字符串连接说明请看msdn:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbconnectionclassconnectionstringtopic.asp?frame=true
system.data.oracleclient.oracleconnection
常用的一些连接字符串(c#代码):
oracleconnection myconn = new system.data.oracleclient.oracleconnection(
"data source=oracle8i;integrated security=yes");
更多字符串连接说明请看msdn:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoracleclientoracleconnectionclassconnectionstringtopic.asp?frame=true
system.data.odbc.odbcconnection
常用的一些连接字符串(c#代码):
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)};dbq=c:/bin/nwind.mdb");
odbcconnection conn = new odbcconnection(
"driver={microsoft excel driver (*.xls)};dbq=c:/bin/book1.xls");
odbcconnection conn = new odbcconnection(
"driver={microsoft text driver (*.txt; *.csv)};dbq=c:/bin");
odbcconnection conn = new odbcconnection("dsn=dsnname");
更多字符串连接说明请看msdn:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataodbcodbcconnectionclassconnectionstringtopic.asp?frame=true
其他厂商提供的数据库连接:
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=<host>;password=<password>;provider=mssql");
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>;provider=db2");
connection pooling
在sql server、ole db和.net框架结构中的data provider中,都提供了隐式的连接池连接支持。你可以在connectionstring中指定不同的参数值控制连接池的行为。比如下面的例子使ole db的连接池无效并自动地进行事务处理:
provider=sqloledb;ole db services=-4;data source=localhost;integrated security=sspi;
在sql server.net data provider中提供了以下参数设置控制连接池的行为:connection lifttime、connection reset、enlist、max pool size、min pool size和pooling。