c#中数据库字符串的连接⼏种⽅式
ADO 中数据库连接⽅式(微软提供)
微软提供了以下四种数据库连接⽅式:
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:
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:
System.Data.OracleClient.OracleConnection
常⽤的⼀些连接字符串(C#代码):
OracleConnection myConn = new System.Data.OracleClient.OracleConnection(
"Data Source=Oracle8i;Integrated Security=yes");
更多字符串连接说明请看MSDN:
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:
其他⼚商提供的数据库连接:
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。
更多数据库连接信息,以及⾮ADO的连接字符串可以参看:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。