介绍几种 ADO.net 中的数据库连接方式

前端之家收集整理的这篇文章主要介绍了介绍几种 ADO.net 中的数据库连接方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在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=; 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=;usernam   e=;hostname=;password=;provider=MSsql");      BdpConnection myConn = new Borland.Data.Provider.BdpConnection("assembly=Borl   and.Data.Db2,PublicKeyToken=91d62ebb5b0d1b1b;ve   ndorclient=db2cli.dll;database=;username=;   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。                                                                                                          本文转载自‘中国IT实验室’

原文链接:https://www.f2er.com/aspnet/74558.html

猜你在找的asp.Net相关文章