ado.net 連線資料庫

iDotNetSpace發表於2008-09-24

 
ado.net 連線資料庫
2008-03-14 23:22
在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=SS
PI;database=Pubs");

SqlConnection conn
= new SqlConnection("server=(local)\\NetSDK;database=pubs;I
ntegrated Security=SSPI");

SqlConnection conn = new SqlConnection(
"Data Source=localhost;Integrated Security=SSPI;Initial Cat
alog=Northwind;");

SqlConnection conn = new SqlConnection(
" data source=(local);initial catalog=xr;integrated securit
y=SSPI;
persist security info=False;workstation id=XURUI;packet size=409
6; ");

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/frlrfSystemDataSqlClientSqlConnectionClassConnection
StringTopic.asp

System.Data.OleDb.OleDbConnection
常用的一些連線字串(C#程式碼):

OleDbConnection conn = new OleDbConnection(@"Provider=Micro
soft.Jet.OLEDB.4.0;Data Source=D:\MyWeb\81\05\GrocerToGo.mdb&quo
t;);

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 I
nfo=False;Integrated Security=yes");

OleDbConnection conn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\bin\Local
Access40.mdb");

OleDbConnection conn = new OleDbConnection(
"Provider=SQLOLEDB;Data Source=MySQLServer;Integrated Secur
ity=SSPI");

更多字串連線說明請看MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us
/cpref/html/frlrfSystemDataOleDbOleDbConnectionClassConnectionSt
ringTopic.asp?frame=true


System.Data.OracleClient.OracleConnection
常用的一些連線字串(C#程式碼):

OracleConnection myConn = new System.Data.OracleClient.OracleCon
nection(
"Data Source=Oracle8i;Integrated Security=yes");

 

更多字串連線說明請看MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us
/cpref/html/frlrfSystemDataOracleClientOracleConnectionClassConn
ectionStringTopic.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.x
ls");


OdbcConnection conn = new OdbcConnection(
"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin&q
uot;);

OdbcConnection conn = new OdbcConnection("DSN=dsnname"
);

更多字串連線說明請看MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us
/cpref/html/frlrfSystemDataOdbcOdbcConnectionClassConnectionStri
ngTopic.asp?frame=true


其他廠商提供的資料庫連線:

DB2Connection myConn = new IBM.Data.DB2.DB2Connection(
"DATABASE = SAMPLE;UID=; PWD=t;;");

DB2Connection myConn = new IBM.Data.DB2.DB2Connection("DATA
BASE = SAMPLE");


BdpConnection myConn = new Borland.Data.Provider.BdpConnection(&
quot;assembly=Borl
and.Data.Mssql,Version=1.1.0.0,Culture=neutral,PublicKeyToken=91
d62ebb5b0d1b1b;ve
ndorclient=sqloledb.dll;osauthentication=False;database=base>;usernam
e=;hostname=;password=;p
rovider=MSSQL");

BdpConnection myConn = new Borland.Data.Provider.BdpConnection(&
quot;assembly=Borl
and.Data.Db2,Version=1.1.0.0,Culture=neutral,PublicKeyToken=91d6
2ebb5b0d1b1b;ve
ndorclient=db2cli.dll;database=;username=r>;
password=;provider=DB2");


Connection Pooling


在SQL Server、OLE DB和.NET框架結構中的Data Provider中,都提供了隱
式的連線池連線支援。你可以在ConnectionString中指定不同的引數值控
制連線池的行為。比如下面的例子使OLE DB的連線池無效並自動地進行事
務處理:
Provider=SQLOLEDB;OLE DB Services=-4;Data Source=localhost;Integ
rated Security在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=SS
PI;database=Pubs");

SqlConnection conn
= new SqlConnection("server=(local)\\NetSDK;database=pubs;I
ntegrated Security=SSPI");

SqlConnection conn = new SqlConnection(
"Data Source=localhost;Integrated Security=SSPI;Initial Cat
alog=Northwind;");

SqlConnection conn = new SqlConnection(
" data source=(local);initial catalog=xr;integrated securit
y=SSPI;
persist security info=False;workstation id=XURUI;packet size=409
6; ");

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/frlrfSystemDataSqlClientSqlConnectionClassConnection
StringTopic.asp

System.Data.OleDb.OleDbConnection
常用的一些連線字串(C#程式碼):

OleDbConnection conn = new OleDbConnection(@"Provider=Micro
soft.Jet.OLEDB.4.0;Data Source=D:\MyWeb\81\05\GrocerToGo.mdb&quo
t;);

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 I
nfo=False;Integrated Security=yes");

OleDbConnection conn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\bin\Local
Access40.mdb");

OleDbConnection conn = new OleDbConnection(
"Provider=SQLOLEDB;Data Source=MySQLServer;Integrated Secur
ity=SSPI");

更多字串連線說明請看MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us
/cpref/html/frlrfSystemDataOleDbOleDbConnectionClassConnectionSt
ringTopic.asp?frame=true


System.Data.OracleClient.OracleConnection
常用的一些連線字串(C#程式碼):

OracleConnection myConn = new System.Data.OracleClient.OracleCon
nection(
"Data Source=Oracle8i;Integrated Security=yes");

 

更多字串連線說明請看MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us
/cpref/html/frlrfSystemDataOracleClientOracleConnectionClassConn
ectionStringTopic.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.x
ls");


OdbcConnection conn = new OdbcConnection(
"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin&q
uot;);

OdbcConnection conn = new OdbcConnection("DSN=dsnname"
);

更多字串連線說明請看MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us
/cpref/html/frlrfSystemDataOdbcOdbcConnectionClassConnectionStri
ngTopic.asp?frame=true


其他廠商提供的資料庫連線:

DB2Connection myConn = new IBM.Data.DB2.DB2Connection(
"DATABASE = SAMPLE;UID=; PWD=t;;");

DB2Connection myConn = new IBM.Data.DB2.DB2Connection("DATA
BASE = SAMPLE");


BdpConnection myConn = new Borland.Data.Provider.BdpConnection(&
quot;assembly=Borl
and.Data.Mssql,Version=1.1.0.0,Culture=neutral,PublicKeyToken=91
d62ebb5b0d1b1b;ve
ndorclient=sqloledb.dll;osauthentication=False;database=base>;usernam
e=;hostname=;password=;p
rovider=MSSQL");

BdpConnection myConn = new Borland.Data.Provider.BdpConnection(&
quot;assembly=Borl
and.Data.Db2,Version=1.1.0.0,Culture=neutral,PublicKeyToken=91d6
2ebb5b0d1b1b;ve
ndorclient=db2cli.dll;database=;username=r>;
password=;provider=DB2");


Connection Pooling


在SQL Server、OLE DB和.NET框架結構中的Data Provider中,都提供了隱
式的連線池連線支援。你可以在ConnectionString中指定不同的引數值控
制連線池的行為。比如下面的例子使OLE DB的連線池無效並自動地進行事
務處理:
Provider=SQLOLEDB;OLE DB Services=-4;Data Source=localhost;Integ
rated 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/
=SSPI;
在SQL Server.NET Data Provider中提供了以下引數設定控制連線池的行
為:Connection Lifttime、Connection Reset、Enlist、Max Pool Size
、Min Pool Size和Pooling。

更多資料庫連線資訊,以及非ADO.net的連線字串可以參看:
http://www.connectionstrings.com/ 

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-462845/,如需轉載,請註明出處,否則將追究法律責任。

相關文章