微软提供了以下四种数据库连接方式: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); 综合安全= SSPI; database = Pubs“);
SqlConnection conn
= new SqlConnection(“server =(local)\\ NetSDK; database = pubs; 综合安全= SSPI“);
SqlConnection conn = new SqlConnection(
“Data Source = localhost; 综合安全= SSPI; 初始目录= Northwind;“);
SqlConnection conn = new SqlConnection(
“data source =(local); 初始目录= xr; 综合安全= SSPI;
坚持安全信息=假; workstation id = XURUI; 包大小= 4096;“);
SqlConnection myConn = new
System.Data.SqlClient.SqlConnection(“Persist Security Info = False; 综合
安全= SSPI; database = northwind; server = mySQLServer“);
SqlConnection conn = new SqlConnection(
“uid = sa; pwd =密码; 初始目录= pubs; 数据源= 127.0.0.1; 连接超时= 900“);
字符串更多连接说明请看 MSDN: HTTP:
//msdn.microsoft.com/library/default.asp?url = /库/ EN-US / CPREF / HTML / frlrfSystemDataSqlClient SqlConnectionClassConnec tionStringTopic.asp
System.Data.OleDb.OleDbConnection
常用的一些连接字符串(C#代码):
OleDbConnection conn = new OleDbConnection(@“Provider = Microsoft.Jet.OLEDB.4.0; 数据源= D:\ MyWeb \ 81 \ 05 \ GrocerToGo.mdb“);
OleDbConnection conn = new OleDbConnection(
@“Provider = Microsoft.Jet.OLEDB.4.0; 密码=;
用户ID = Admin; 数据源= grocertogo.mdb;“);
OleDbConnection conn = new OleDbConnection(
“Provider = MSDAORA; 数据源= ORACLE8i7; 坚持安全信息=错误; 综合安全=是“);
OleDbConnection conn = new OleDbConnection(
“Provider = Microsoft.Jet.OLEDB.4.0; 数据源= c:\ bin \ LocalAccess40.mdb“);
OleDbConnection conn = new OleDbConnection(
“Provider = SQLOLEDB; 数据源= MySQLServer; 综合安全= SSPI“);
字符串更多连接说明请看 MSDN: HTTP:
??//msdn.microsoft.com/library/default.asp URL = /库/ EN-US / CPREF / HTML / frlrfSystemDataOleDbOleD bConnectionClassConnecti onStringTopic.asp帧=真
System.Data.OracleClient.OracleConnection
常用的一些连接字符串(C#代码):
OracleConnection myConn = new System.Data.OracleClient.OracleConnection(
“Data Source = Oracle8i; 综合安全=是“);
字符串更多连接说明请看 MSDN: HTTP:
??//msdn.microsoft.com/library/default.asp URL = /库/ EN-US / CPREF / HTML / frlrfSystemDataOracleCli entOracleConnectionClass ConnectionStringTopic.asp帧=真
System.Data.Odbc.OdbcConnection
常用的一些连接字符串(C#代码):
OdbcConnection conn = new OdbcConnection(
“Driver = {SQL Server}; Server = MyServer; Trusted_Connection =是; Database = Northwind;“);
OdbcConnection conn = new OdbcConnection(
“Driver = {Microsoft ODBC for Oracle}; Server = ORACLE8i7;
坚持安全信息=错误; 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 / frlrfSystemDataOdbcOdbcC onnectionClassConnection StringTopic.asp?frame = true
其他厂商提供的数据库连接:
DB2Connection myConn = new IBM.Data.DB2.DB2Connection(
“DATABASE = SAMPLE; UID = <用户名>; PWD = <密码>;“);
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 =
e =
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 =
password = <密码>; provider = DB2“);
连接池
在SQL Server,OLE DB和.NET框架结构中的数据提供者中,都提供了隐式的连接池连接支持。你可以在ConnectionString中指定不同的参数值控制连接池的行为。比如下面的例子使用OLE DB的连接池无效并自动地进行事务处理:
Provider = SQLOLEDB; OLE DB服务= -4; 数据源= localhost; 集成安全性= SSPI;
在SQL Server.NET数据提供者中提供了以下参数设置控制连接池的行为:连接提升时间,连接重置,登记,最大池大小,最小池大小和池。
更多数据库连接信息,以及非ADO.net的连接字符串可以参看:http:
//www.connectionstrings.com/
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiaokexinger/archive/2007/03/26/1541441.aspx