程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ADO.net中數據庫連接方式

ADO.net中數據庫連接方式

編輯:.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=<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.Net的連接字符串可以參看:
http://www.connectionstrings.com/

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved