转自:http://blog.csdn.net/xiaokexinger/article/details/1541441(部分做了补充)
在MSDN中,.net的数据库连接字符串都有详细的说明,我这里以代码范例的方式罗列一些,具体的每一项代表的意义可以参看MSDN.
ADO.net 中数据库连接方式(微软提供)微软提供了以下四种数据库连接方式:
System.Data.OleDb.OleDbConnection
System.Data.SqlClient.SqlConnection
System.Data.Odbc.OdbcConnection
System.Data.OracleClient.OracleConnection
个人理解:
OLE DB类似于全能选手(可以连接多种数据库类型),不过在连接时要指明"Provider";
ODBC比较全能,但没有OLE DB支持的数据库类型多
SQL/Oracle则类似于专业选手(只适用于各自的数据库),效率比OLE DB要高一些
同样的,SqlDataAdapter,SqlCommand是专门用于(MS SQL)数据库;而OleDbDataAdapter, OleDbCommand适用于所有的数据库
注意:
如果使用的是SqlConnection,那么在今后的数据库操作中你只能用SqlDataAdapter,SqlCommand
如果使用的是OleDbConnection,那么在今后的数据库操作中你只能用OleDbDataAdapter,OleDbCommand
其他的同样道理;
补充:OBDC与OLE DB的关系(来自百度百科)
OLE DB 和ODBC 标准都提供了统一的访问数据接口。ODBC 标准的对象是基于SQL 的数据源(SQL-Based Data Source),而OLE DB 的对象则是范围更为广泛的任何数据存储。
注意:
下面我们以范例的方式,来依次说明:System.Data.SqlClient.SqlConnection
常用的一些连接字符串(C#代码):
(1) SqlConnection conn = new SqlConnection( "Server=(local);Integrated Security=SSPI;database=Pubs");
(2) SqlConnection conn = new SqlConnection("server=(local)//NetSDK;database=pubs;Integrated Security=SSPI");
(3) SqlConnection conn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");
(4) SqlConnection conn = new SqlConnection(" data source=(local);initial catalog=xr;integrated security=SSPI;
persist security info=False;workstation id=XURUI;packet size=4096; ");
(5) SqlConnection myConn = new System.Data.SqlClient.SqlConnection("Persist SecurityInfo=False;Integrated Security=SSPI;database=northwind;server=mySQLServer");
(6) SqlConnection conn = new SqlConnection(" uid=sa;pwd=passwords;initial catalog=pubs;data source=127.0.0.1;Connect Timeout=900");
其中,"server"表示要连接的数据库服务器;“Integrated Security”使用Windows Authentication连接数据库;“database”指定要连接的数据库实例;
更多字符串连接说明请看MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlConnectionClassConnectionStringTopic.asp
System.Data.OleDb.OleDbConnection常用的一些连接字符串(C#代码):
(1) OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/MyWeb/81/05/GrocerToGo.mdb");
(2) OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Password=; User ID=Admin;Data Source=grocertogo.mdb;");
(3) OleDbConnection conn = new OleDbConnection("Provider=MSDAORA; Data Source=ORACLE8i7;Persist Security Info=False;Integrated Security=yes");
(4) OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:/bin/LocalAccess40.mdb");
(5) 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#代码):
(1) OdbcConnection conn = new OdbcConnection("Driver={SQL Server};Server=MyServer;Trusted_Connection=yes;Database=Northwind;");
(2) OdbcConnection conn = new OdbcConnection("Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;Persist Security Info=False;Trusted_Connection=yes");
(3) OdbcConnection conn = new OdbcConnection("Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/bin/nwind.mdb");
(4) OdbcConnection conn = new OdbcConnection("Driver={Microsoft Excel Driver (*.xls)};DBQ=c:/bin/book1.xls");
(5) OdbcConnection conn = new OdbcConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:/bin");
(6) 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=Borland.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;vendorclient=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.net的连接字符串可以参看:http://www.connectionstrings.com/