c#

位置:IT落伍者 >> c# >> 浏览文章

ADO.net中数据库连接方式


发布日期:2023年05月04日
 
ADO.net中数据库连接方式

在MSDN中net的数据库连接字符串都有详细的说明我这里以代码范例的方式罗列一些具体的每一项代表的意义可以参看MSDN

中数据库连接方式(微软提供)

微软提供了以下四种数据库连接方式

SystemDataOleDbOleDbConnection

SystemDataSqlClientSqlConnection

SystemDataOdbcOdbcConnection

SystemDataOracleClientOracleConnection

下面我们以范例的方式来依次说明

SystemDataSqlClientSqlConnection

常用的一些连接字符串(C#代码)

SqlConnection conn

= new SqlConnection( Server=(local)Integrated Security=SSPIdatabase=Pubs

SqlConnection conn

= new SqlConnection(server=(local)\\NetSDKdatabase=pubsIntegrated Security=SSPI

SqlConnection conn = new SqlConnection(

Data Source=localhostIntegrated Security=SSPIInitial Catalog=Northwind

SqlConnection conn = new SqlConnection(

data source=(local)initial catalog=xrintegrated security=SSPI

persist security info=Falseworkstation id=XURUIpacket size=

SqlConnection myConn = new

SystemDataSqlClientSqlConnection(Persist Security Info=FalseIntegrated

Security=SSPIdatabase=northwindserver=mySQLServer

SqlConnection conn = new SqlConnection(

uid=sapwd=passwordsinitial catalog=pubsdata source=Connect Timeout=

更多字符串连接说明请看MSDN

?url=/library/enus/cpref/html/frlrfSystemDataSqlClientSqlConnectionClassConnectionStringTopicasp

SystemDataOleDbOleDbConnection

常用的一些连接字符串(C#代码)

OleDbConnection conn = new OleDbConnection(@Provider=MicrosoftJetOLEDBData Source=D\MyWeb\\\GrocerToGomdb

OleDbConnection conn = new OleDbConnection(

@Provider=MicrosoftJetOLEDBPassword=

User ID=AdminData Source=grocertogomdb

OleDbConnection conn = new OleDbConnection(

Provider=MSDAORA Data Source=ORACLEiPersist Security Info=FalseIntegrated Security=yes

OleDbConnection conn = new OleDbConnection(

Provider=MicrosoftJetOLEDB Data Source=c\bin\LocalAccessmdb

OleDbConnection conn = new OleDbConnection(

Provider=SQLOLEDBData Source=MySQLServerIntegrated Security=SSPI

更多字符串连接说明请看MSDN

?url=/library/enus/cpref/html/frlrfSystemDataOleDbOleDbConnectionClassConnectionStringTopicasp?frame=true

SystemDataOracleClientOracleConnection

常用的一些连接字符串(C#代码)

OracleConnection myConn = new SystemDataOracleClientOracleConnection(

Data Source=OracleiIntegrated Security=yes

更多字符串连接说明请看MSDN

?url=/library/enus/cpref/html/frlrfSystemDataOracleClientOracleConnectionClassConnectionStringTopicasp?frame=true

SystemDataOdbcOdbcConnection

常用的一些连接字符串(C#代码)

OdbcConnection conn = new OdbcConnection(

Driver={SQL Server}Server=MyServerTrusted_Connection=yesDatabase=Northwind

OdbcConnection conn = new OdbcConnection(

Driver={Microsoft ODBC for Oracle}Server=ORACLEi

Persist Security Info=FalseTrusted_Connection=yes

OdbcConnection conn = new OdbcConnection(

Driver={Microsoft Access Driver (*mdb)}DBQ=c\bin\nwindmdb

OdbcConnection conn = new OdbcConnection(

Driver={Microsoft Excel Driver (*xls)}DBQ=c\bin\bookxls

OdbcConnection conn = new OdbcConnection(

Driver={Microsoft Text Driver (*txt *csv)}DBQ=c\bin

OdbcConnection conn = new OdbcConnection(DSN=dsnname

更多字符串连接说明请看MSDN

?url=/library/enus/cpref/html/frlrfSystemDataOdbcOdbcConnectionClassConnectionStringTopicasp?frame=true

其他厂商提供的数据库连接

DBConnection myConn = new IBMDataDBDBConnection(

DATABASE = SAMPLEUID=<username> PWD=<password>

DBConnection myConn = new IBMDataDBDBConnection(DATABASE = SAMPLE

BdpConnection myConn = new BorlandDataProviderBdpConnection(assembly=Borl

andDataMssqlVersion=Culture=neutralPublicKeyToken=debbbdbbve

ndorclient=sqloledbdllosauthentication=Falsedatabase=<database>usernam

e=<user>hostname=<host>password=<password>provider=MSSQL

BdpConnection myConn = new BorlandDataProviderBdpConnection(assembly=Borl

andDataDbVersion=Culture=neutralPublicKeyToken=debbbdbbve

ndorclient=dbclidlldatabase=<database>username=<user>

password=<password>provider=DB

Connection Pooling

在SQL ServerOLE DB和NET框架结构中的Data Provider中都提供了隐式的连接池连接支持你可以在ConnectionString中指定不同的参数值控制连接池的行为比如下面的例子使OLE DB的连接池无效并自动地进行事务处理

Provider=SQLOLEDBOLE DB Services=Data Source=localhostIntegrated Security=SSPI

在SQL ServerNET Data Provider中提供了以下参数设置控制连接池的行为Connection LifttimeConnection ResetEnlistMax Pool SizeMin Pool Size和Pooling

上一篇:.Net Framework 2.0正式版发布

下一篇:C#中的DBNull、Null和String.Empty解释