最大的網(wǎng)站源碼資源下載站,
    在商業(yè)應(yīng)用程序中最重要的組件是數(shù)據(jù),無(wú)論是在線的商務(wù)應(yīng)用程序、公司的企業(yè)軟件還是小型公司的會(huì)計(jì)應(yīng)用程序無(wú)不如此:通過(guò)一個(gè)通用的線程與數(shù)據(jù)打交道,即都必須實(shí)現(xiàn)快速、有效、可靠的方式存儲(chǔ)、檢索和處理數(shù)據(jù)。 
   
  然而,一直一來(lái)令人棘手的問(wèn)題是這些數(shù)據(jù)文件常以不同的格式存儲(chǔ),這就需要開(kāi)發(fā)者學(xué)會(huì)用多種不同的方式來(lái)處理完全一樣的事情。microsoft等諸多數(shù)據(jù)提供者力求實(shí)現(xiàn)數(shù)據(jù)訪問(wèn)格式的標(biāo)準(zhǔn)化。 
   
  從odbc的出現(xiàn)到dao、rdo、oledb、ado的實(shí)現(xiàn),可以說(shuō)標(biāo)準(zhǔn)化的進(jìn)程逐步實(shí)現(xiàn)。特別是ado的出現(xiàn)很好的實(shí)現(xiàn)了通用數(shù)據(jù)訪問(wèn)的模式,致使很多人認(rèn)為,每隔兩年學(xué)習(xí)一種新的數(shù)據(jù)訪問(wèn)對(duì)象模型的年代已經(jīng)過(guò)去了。但是通過(guò)internet,我們發(fā)現(xiàn)這種想法是錯(cuò)誤的。 
   
  一、ado.net 的設(shè)計(jì)目標(biāo) 
   
  隨著應(yīng)用程序開(kāi)發(fā)的發(fā)展演變,新的應(yīng)用程序已基于 web 應(yīng)用程序模型越來(lái)越松散地耦合。如今,越來(lái)越多的應(yīng)用程序使用 xml 來(lái)編碼要通過(guò)網(wǎng)絡(luò)連接傳遞的數(shù)據(jù)。web 應(yīng)用程序?qū)?http 用作在層間進(jìn)行通信的結(jié)構(gòu),因此它們必須顯式處理請(qǐng)求之間的狀態(tài)維護(hù)。這一新模型大大不同于連接、緊耦合的編程風(fēng)格,此風(fēng)格曾是客戶端/服務(wù)器時(shí)代的標(biāo)志。在此編程風(fēng)格中,連接會(huì)在程序的整個(gè)生存期中保持打開(kāi),而不需要對(duì)狀態(tài)進(jìn)行特殊處理。 
   
  設(shè)計(jì) ado.net 的目的是為了滿足這一新編程模型的以下要求:具有斷開(kāi)式數(shù)據(jù)結(jié)構(gòu);能夠與 xml 緊密集成;具有能夠組合來(lái)自多個(gè)、不同數(shù)據(jù)源的數(shù)據(jù)的通用數(shù)據(jù)表示形式;在創(chuàng)建 ado.net 時(shí),microsoft 具有以下設(shè)計(jì)目標(biāo)。 
   
  利用當(dāng)前的 ado 知識(shí) 
   
  ado.net 的設(shè)計(jì)滿足了當(dāng)今應(yīng)用程序開(kāi)發(fā)模型的多種要求。同時(shí),該編程模型盡可能地與 ado 保持一致,這使當(dāng)今的 ado 開(kāi)發(fā)人員不必從頭開(kāi)始學(xué)習(xí)全新的數(shù)據(jù)訪問(wèn)技術(shù)。ado.net 是 .net framework 的固有部分,因此對(duì)于 ado 程序員決不是完全陌生的。 
   
  ado.net 與 ado 共存。雖然大多數(shù)基于 .net 的新應(yīng)用程序?qū)⑹褂?ado.net 來(lái)編寫(xiě),但 .net 程序員仍然可以通過(guò) .net com 互操作性服務(wù)來(lái)使用 ado。 
   
  支持 n 層編程模式 
  ado.net 為斷開(kāi)式 n 層編程環(huán)境提供了一流的支持,許多新的應(yīng)用程序都是為該環(huán)境編寫(xiě)的。使用斷開(kāi)式數(shù)據(jù)集這一概念已成為編程模型中的焦點(diǎn)。n 層編程的 ado.net 解決方案就是 dataset。 
   
  集成 xml 支持 
  xml 和數(shù)據(jù)訪問(wèn)是緊密聯(lián)系在一起的,即 xml 的全部?jī)?nèi)容都是有關(guān)數(shù)據(jù)編碼的,而數(shù)據(jù)訪問(wèn)越來(lái)越多的內(nèi)容都與 xml 有關(guān)。.net framework 不僅支持 web 標(biāo)準(zhǔn),它還是完全基于 web 標(biāo)準(zhǔn)生成的。 
   
  xml 支持內(nèi)置在 ado.net 中非常基本的級(jí)別上。.net framework 和 ado.net 中的 xml 類是同一結(jié)構(gòu)的一部分,它們?cè)谠S多不同的級(jí)別集成。您不必在數(shù)據(jù)訪問(wèn)服務(wù)集和它們的 xml 相應(yīng)服務(wù)之間進(jìn)行選擇;它們的設(shè)計(jì)本來(lái)就具有從其中一個(gè)跨越到另一個(gè)的功能。
   二、ado.net 的組件 
   
  設(shè)計(jì) ado.net 組件的目的是為了從數(shù)據(jù)操作中分解出數(shù)據(jù)訪問(wèn)。ado.net 的兩個(gè)核心組件會(huì)完成此任務(wù):dataset 和 .net framework 數(shù)據(jù)提供程序,后者是一組包括 connection、command、datareader 和 dataadapter 對(duì)象在內(nèi)的組件。 
   
  ado.net dataset 是 ado.net 的斷開(kāi)式結(jié)構(gòu)的核心組件。dataset 的設(shè)計(jì)目的很明確:為了實(shí)現(xiàn)獨(dú)立于任何數(shù)據(jù)源的數(shù)據(jù)訪問(wèn)。因此,它可以用于多種不同的數(shù)據(jù)源,用于 xml 數(shù)據(jù),或用于管理應(yīng)用程序本地的數(shù)據(jù)。dataset 包含一個(gè)或多個(gè) datatable 對(duì)象的集合,這些對(duì)象由數(shù)據(jù)行和數(shù)據(jù)列以及主鍵、外鍵、約束和有關(guān) datatable 對(duì)象中數(shù)據(jù)的關(guān)系信息組成。 
   
  ado.net 結(jié)構(gòu)的另一個(gè)核心元素是 .net framework 數(shù)據(jù)提供程序,其組件的設(shè)計(jì)目的相當(dāng)明確:為了實(shí)現(xiàn)數(shù)據(jù)操作和對(duì)數(shù)據(jù)的快速、只進(jìn)、只讀訪問(wèn)。connection 對(duì)象提供與數(shù)據(jù)源的連接。command 對(duì)象使您能夠訪問(wèn)用于返回?cái)?shù)據(jù)、修改數(shù)據(jù)、運(yùn)行存儲(chǔ)過(guò)程以及發(fā)送或檢索參數(shù)信息的數(shù)據(jù)庫(kù)命令。datareader 從數(shù)據(jù)源中提供高性能的數(shù)據(jù)流。最后,dataadapter 提供連接 dataset 對(duì)象和數(shù)據(jù)源的橋梁。dataadapter 使用 command 對(duì)象在數(shù)據(jù)源中執(zhí)行 sql 命令,以便將數(shù)據(jù)加載到 dataset 中,并使對(duì) dataset 中數(shù)據(jù)的更改與數(shù)據(jù)源保持一致。 
   
  可以為任何數(shù)據(jù)源編寫(xiě) .net framework 數(shù)據(jù)提供程序。.net framework 提供了四個(gè) .net framework 數(shù)據(jù)提供程序:sql server .net framework 數(shù)據(jù)提供程序、ole db .net framework 數(shù)據(jù)提供程序、odbc .net framework 數(shù)據(jù)提供程序和 oracle .net framework 數(shù)據(jù)提供程序。 
   
  下圖闡釋了 ado.net 結(jié)構(gòu)的組件。 
   
  三、使用 ado.net 連接到數(shù)據(jù)源 
  在 ado.net 中,可以使用 connection 對(duì)象來(lái)連接到指定的數(shù)據(jù)源。若要連接到 microsoft sql server 7.0 版或更高版本,請(qǐng)使用 sql server .net framework 數(shù)據(jù)提供程序的 sqlconnection 對(duì)象。若要使用用于 sql server 的 ole db 提供程序 (sqloledb) 連接到 ole db 數(shù)據(jù)源或者連接到 microsoft sql server 6.x 版或較早版本,請(qǐng)使用 ole db .net framework 數(shù)據(jù)提供程序的 oledbconnection 對(duì)象。若要連接到 odbc 數(shù)據(jù)源,請(qǐng)使用 odbc .net framework 數(shù)據(jù)提供程序的 odbcconnection 對(duì)象。若要連接到 oracle 數(shù)據(jù)源,請(qǐng)使用 oracle .net framework 數(shù)據(jù)提供程序的 oracleconnection 對(duì)象。 
   
  1、 使用 ado.net 連接到 sql server 
  sql server .net framework 數(shù)據(jù)提供程序使用 sqlconnection 對(duì)象提供與 microsoft sql server 7.0 版或更高版本的連接。 
   
  sql server .net framework 數(shù)據(jù)提供程序支持類似于 ole db (ado) 連接字符串格式的連接字符串格式。有關(guān)有效的字符串格式名稱和值,請(qǐng)參見(jiàn)附表1 
   
  以下代碼示例演示如何創(chuàng)建和打開(kāi)與 sql server(版本 7.0 或更高版本)數(shù)據(jù)庫(kù)的連接。 
   
  [visual basic] 
  dim myconn as sqlconnection = new sqlconnection("data source=localhost;integrated security=sspi;initial catalog=northwind") 
  myconn.open() 
  [c#] 
  sqlconnection nwindconn = new sqlconnection("data source=localhost; integrated security=sspi;" + 
  "initial catalog=northwind"); 
  nwindconn.open(); 
  建議使用完 connection 后始終將其關(guān)閉。這可以使用 connection 對(duì)象的 close 或 dispose 方法來(lái)實(shí)現(xiàn)。 
   
  集成安全性和 asp.net 
  sql server 集成安全性(也稱為受信任的連接)是連接到 sql server 的最安全的方法,因?yàn)樗辉谶B接字符串中公開(kāi)用戶標(biāo)識(shí)和密碼。建議使用該方法對(duì)連接進(jìn)行身份驗(yàn)證。集成安全性使用正在執(zhí)行的進(jìn)程的當(dāng)前安全標(biāo)識(shí)或標(biāo)記。對(duì)于桌面應(yīng)用程序,安全標(biāo)識(shí)或標(biāo)記通常是當(dāng)前登錄的用戶的標(biāo)識(shí)。 
   
  asp.net 應(yīng)用程序的安全標(biāo)識(shí)可設(shè)置為幾個(gè)不同的選項(xiàng)之一。若要更好地了解使用集成安全性連接到 sql server 時(shí) asp.net 應(yīng)用程序所使用的安全標(biāo)識(shí),請(qǐng)參見(jiàn)我寫(xiě)得asp.net中進(jìn)行安全的 ado.net 編碼系列或者參考msdn 
   
  name 
  default 
  description 
  名稱 
  默認(rèn)值 
  說(shuō)明 
   
  application name 
   
  the name of the application, or '.net sqlclient data provider' if no application name is provided. 
  應(yīng)用程序名稱 
   
  應(yīng)用程序的名稱,或者“.net sqlclient data provider”(如果不提供應(yīng)用程序名稱)。 
   
  attachdbfilename 
   
  -or- 
   
  extended properties 
   
  -or- 
   
  initial file name 
   
  the name of the primary file, including the full path name, of an attachable database. 
   
  the database name must be specified with the keyword 'database'. 
  attachdbfilename 
   
  - 或 - 
   
  擴(kuò)展屬性 
   
  - 或 - 
   
  初始文件名 
   
  可連接數(shù)據(jù)庫(kù)的主文件的名稱,包括完整的路徑名。 
   
  必須使用關(guān)鍵字“database”來(lái)指定數(shù)據(jù)庫(kù)的名稱。 
   
  connect timeout 
   
  -or- 
   
  connection timeout 
  15 
  the length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error. 
  連接超時(shí)設(shè)定 
   
  - 或 - 
   
  連接超時(shí) 
  15 
  在終止嘗試并產(chǎn)生錯(cuò)誤之前,等待與服務(wù)器的連接的時(shí)間長(zhǎng)度(以秒為單位)。 
   
  current language 
   
  the sql server language record name. 
  當(dāng)前語(yǔ)言 
   
  sql server 語(yǔ)言記錄名稱。 
   
  data source 
   
  -or- 
   
  server 
   
  -or- 
   
  address 
   
  -or- 
   
  addr 
   
  -or- 
   
  network address 
   
  the name or network address of the instance of sql server to which to connect. 
  數(shù)據(jù)源 
   
  - 或 - 
   
  服務(wù)器 
   
  - 或 - 
   
  地址 
   
  - 或 - 
   
  addr 
   
  - 或 - 
   
  網(wǎng)絡(luò)地址 
   
  要連接的 sql server 實(shí)例的名稱或網(wǎng)絡(luò)地址。 
   
  encrypt 
  'false' 
  when true, sql server uses ssl encryption for all data sent between the client and server if the server has a certificate installed. recognized values are true, false, yes, and no. 
  加密 
  'false' 
  當(dāng)該值為 true 時(shí),如果服務(wù)器端安裝了證書(shū),則 sql server 將對(duì)所有在客戶端和服務(wù)器之間傳送的數(shù)據(jù)使用 ssl 加密。可識(shí)別的值為 true、false、yes 和 no。 
   
  initial catalog 
   
  -or- 
   
  database 
   
  the name of the database. 
  初始目錄 
   
  - 或 - 
   
  數(shù)據(jù)庫(kù) 
   
  數(shù)據(jù)庫(kù)的名稱。 
   
  integrated security 
   
  -or- 
   
  trusted_connection 
  'false' 
  when false, user id and password are specified in the connection. when true, the current windows account credentials are used for authentication. 
   
  recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true. 
  集成安全性 
   
  - 或 - 
   
  trusted_connection 
  'false' 
  當(dāng)為 false 時(shí),將在連接中指定用戶 id 和密碼。當(dāng)為 true 時(shí),將使用當(dāng)前的 windows 帳戶憑據(jù)進(jìn)行身份驗(yàn)證。 
   
  可識(shí)別的值為 true、false、yes、no 以及與 true 等效的 sspi(強(qiáng)烈推薦)。 
   
  network library 
   
  -or- 
   
  net 
  'dbmssocn' 
  the network library used to establish a connection to an instance of sql server. supported values include dbnmpntw (named pipes), dbmsrpcn (multiprotocol), dbmsadsn (apple talk), dbmsgnet (via), dbmslpcn (shared memory) and dbmsspxn (ipx/spx), and dbmssocn (tcp/ip). 
   
  the corresponding network dll must be installed on the system to which you connect. if you do not specify a network and you use a local server (for example, "." or "(local)"), shared memory is used. 
  網(wǎng)絡(luò)庫(kù) 
   
  - 或 - 
   
  網(wǎng)絡(luò) 
  'dbmssocn' 
  用于建立與 sql server 實(shí)例的連接的網(wǎng)絡(luò)庫(kù)。支持的值包括 dbnmpntw(命名管道)、dbmsrpcn(多協(xié)議)、dbmsadsn (apple talk)、dbmsgnet (via)、dbmslpcn(共享內(nèi)存)及 dbmsspxn (ipx/spx) 和 dbmssocn (tcp/ip)。 
   
  相應(yīng)的網(wǎng)絡(luò) dll 必須安裝在要連接的系統(tǒng)上。如果不指定網(wǎng)絡(luò)而使用一個(gè)本地服務(wù)器(比如“.”或“(local)”),則使用共享內(nèi)存。 
   
  packet size 
  8192 
  size in bytes of the network packets used to communicate with an instance of sql server. 
  數(shù)據(jù)包大小 
  8192 
  用來(lái)與 sql server 的實(shí)例進(jìn)行通訊的網(wǎng)絡(luò)數(shù)據(jù)包的大小,以字節(jié)為單位。 
   
  password 
   
  -or- 
   
  pwd 
   
  the password for the sql server account logging on (not recommended. to maintain a high level of security, it is strongly recommended that you use the integrated security or trusted_connection keyword instead.). 
  密碼 
   
  - 或 - 
   
  pwd 
   
  sql server 帳戶登錄的密碼(建議不要使用。為了維護(hù)最高級(jí)別的安全性,強(qiáng)烈建議改用 integrated security 或 trusted_connection 關(guān)鍵字)。 
   
  persist security info 
  'false' 
  when set to false or no (strongly recommended), security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open state. resetting the connection string resets all connection string values including the password. recognized values are true, false, yes, and no. 
  持續(xù)安全信息 
  'false' 
  當(dāng)該值設(shè)置為 false 或 no(強(qiáng)烈推薦)時(shí),如果連接是打開(kāi)的或者一直處于打開(kāi)狀態(tài),那么安全敏感信息(如密碼)將不會(huì)作為連接的一部分返回。重置連接字符串將重置包括密碼在內(nèi)的所有連接字符串值。可識(shí)別的值為 true、false、yes 和 no。 
   
  user id 
   
  the sql server login account (not recommended. to maintain a high level of security, it is strongly recommended that you use the integrated security or trusted_connection keyword instead.). 
  用戶 id 
   
  sql server 登錄帳戶(建議不要使用。為了維護(hù)最高級(jí)別的安全性,強(qiáng)烈建議改用 integrated security 或 trusted_connection 關(guān)鍵字)。 
   
  workstation id 
  the local computer name 
  the name of the workstation connecting to sql server. 
  工作站 id 
  本地計(jì)算機(jī)名稱 
  連接到 sql server 的工作站的名稱。 
   
   
   
   
  name 
  default 
  description 
  名稱 
  默認(rèn)值 
  說(shuō)明 
   
  connection lifetime 
  0 
  when a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by connection lifetime. this is useful in clustered configurations to force load balancing between a running server and a server just brought online. 
   
  a value of zero (0) causes pooled connections to have the maximum connection timeout. 
  連接生存期 
  0 
  當(dāng)連接被返回到池時(shí),將其創(chuàng)建時(shí)間與當(dāng)前時(shí)間作比較,如果時(shí)間長(zhǎng)度(以秒為單位)超出了由 connection lifetime 指定的值,該連接就會(huì)被銷毀。這在聚集配置中很有用(用于強(qiáng)制執(zhí)行運(yùn)行中的服務(wù)器和剛置于聯(lián)機(jī)狀態(tài)的服務(wù)器之間的負(fù)載平衡)。 
   
  零 (0) 值將使池連接具有最大的連接超時(shí)。 
   
  connection reset 
  'true' 
  determines whether the database connection is reset when being drawn from the pool. for microsoft sql server version 7.0, setting to false avoids making an additional server round trip when obtaining a connection, but you must be aware that the connection state, such as database context, is not being reset. 
  連接重置 
  'true' 
  確定從池中提取數(shù)據(jù)庫(kù)連接時(shí)是否重置數(shù)據(jù)庫(kù)連接。對(duì)于 microsoft sql server 7.0 版,設(shè)置為 false 可避免獲取連接時(shí)再有一次額外的服務(wù)器往返行程,但須注意此時(shí)并未重置連接狀態(tài)(如數(shù)據(jù)庫(kù)上下文)。 
   
  enlist 
  'true' 
  when true, the pooler automatically enlists the connection in the creation thread's current transaction context. recognized values are true, false, yes, and no. 
  登記 
  'true' 
  當(dāng)該值為 true 時(shí),池程序在創(chuàng)建線程的當(dāng)前事務(wù)上下文中自動(dòng)登記連接。可識(shí)別的值為 true、false、yes 和 no。 
   
  max pool size 
  100 
  the maximum number of connections allowed in the pool. 
  最大池大小 
  100 
  池中允許的最大連接數(shù)。 
   
  min pool size 
  0 
  the minimum number of connections allowed in the pool. 
  最小池大小 
  0 
  池中允許的最小連接數(shù)。 
   
  pooling 
  'true' 
  when true, the sqlconnection object is drawn from the appropriate pool, or if necessary, is created and added to the appropriate pool. recognized values are true, false, yes, and no. 
  池 
  'true' 
  當(dāng)該值為 true 時(shí),系統(tǒng)將從相應(yīng)池中提取 sqlconnection 對(duì)象,或在必要時(shí)創(chuàng)建該對(duì)象并將其添加到相應(yīng)池中。可識(shí)別的值為 true、false、yes 和 no。
新聞熱點(diǎn)
疑難解答
圖片精選