乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      ASP.NET中使用web.config配置web應用程序中的數(shù)據(jù)庫連接--西部E網(wǎng)wes...

       學海無疆 2011-02-18
      ASP.NET中使用web.config配置web應用程序中的數(shù)據(jù)庫連接
      作者:boyking 發(fā)布時間:2004-9-1 14:57:01 文章來源:boyking's Blog
      你會經(jīng)常碰到這種情況:幾乎在網(wǎng)站的每個頁面上,存儲一些全局處理信息。理想的做法是將這些信息一次性的集中存儲在資料檔案庫中,而不是在網(wǎng)站的每個頁面上都重復這樣的操作。比如說數(shù)據(jù)庫連接串就是這樣的信息,如果這些信息不是集中存儲在特定區(qū)域中,而是在網(wǎng)站的每個需要連接數(shù)據(jù)庫的頁面上手工輸入,可以設想:當數(shù)據(jù)庫連接串改動時將會令人頭痛,你必須遍歷網(wǎng)站中所有連接數(shù)據(jù)庫的頁面去修改!
      在ASP.NET中,通過Web.config,你可為使用 <appSettings> 標記,在這個標記中,你可用 <add ... /> 標記定義0到多個設置。本文中我們主要討論了如何使用web.config來配置一個web應用程序中的數(shù)據(jù)庫連接。

      web.config文件是標準的xml文件,我們可以使用它來為一臺機器下的每一個web應用程序或某個應用程序或一個目錄下的asp.net頁面來進行設置,當然,它也可以為一個單獨的web頁面進行設置。

      如:網(wǎng)站的主目錄是\inetpub\wwwroot\,那么我們將web.config放置于其下,那么這個網(wǎng)站中的應用程序?qū)⒈粀eb.config中的設置所影響。
      e.g.:
      <?xml version="1.0" encoding="gb2312" ?>
      <configuration>
       <system.web>
        <compilation defaultlanguage="vb" debug="true" />
        <customerrors mode="remoteonly" defaultredirect="js/error.htm">
         <error statuscode="404" redirect="js/filenotfound.aspx" />
         <error statuscode="500" redirect="js/error.htm" />
        </customerrors>
        <authentication mode="windows" />
        <authorization>
         <allow users="*" />
        </authorization>
        <httpruntime maxrequestlength="4000" usefullyqualifiedredirecturl="true" executiontimeout="45" />
        <trace enabled="false" requestlimit="10" pageoutput="false" tracemode="sortbytime" localonly="true" />
        <sessionstate mode="inproc" stateconnectionstring="tcpip=127.0.0.1:43444" cookieless="false" timeout="20" />
        <globalization requestencoding="gb2312" responseencoding="gb2312" fileencoding="gb2312" />
       </system.web>
       <appsettings>
        <add key="connstring" value="uid=flash;password=3.1415926;database=news;server=(local)" />
       </appsettings>
      </configuration>

      這里我們討論一下如何在web.config中設置數(shù)據(jù)庫連接。

      1、連接一個數(shù)據(jù)庫:
      在web.config中的<configuration>后加入

      <appsettings>
          <add key="connstring" 
          value="uid=flash;password=3.1415926;database=news;server=(local)" />
      </appsettings>

      在程序中,你可以使用以下代碼來使用web.config中的設置:

      -----vb.net-----
      imports system.configuration
      dim myvar as string 
       myvar=configurationsettings.appsettings("connstring"
      -----c#-----
      using system.configuration;
      string myvar;
      myvar=configurationsettings.appsettings["connstring"];

      2、連接多個數(shù)據(jù)庫
      同理,那就是使用多個不同的key值來設置

      3、設置不同子目錄下應用程序的數(shù)據(jù)庫鏈接
      這是一個很有意思的方法,在設置前,先說明一下它的用途:
      如果在一個虛擬目錄下有多個子目錄,每一個子目錄下下的web應用程序都需要連接不同的數(shù)據(jù)庫,這如何做呢??
      一種方法是在每一個子目錄下分別建立一個web.config,用它來設置這個目錄下的數(shù)據(jù)庫連接。但這種方法的問題是需要維護每一個了目錄下的web.config。

      方法二,是只在虛擬目錄下建立一個web.config,在它里面設置每一個子目錄下的應用程序的數(shù)據(jù)庫連接。說到這里,你會想到上面的第二種方法,使用多個不同的key值來設置,這的確是一個辦法。

      這里,我想說明的是另一種方法:在虛擬目錄下布置web.config,在其中使用location標記,使用同一個key值來連接數(shù)據(jù)庫,這樣做的好處很明顯,因為用同一個key值,將導致在所有目錄下的應用程序中,都可以使用共同的語句來連接數(shù)據(jù)庫,這在程序以后發(fā)生位置遷移時,并不用修改程序中連接數(shù)據(jù)庫的語句。
      具體設置如下:

      <location path="news">
      <appsettings>
       <add key="connstring" value="uid=flyangel;password=3.1415926;database=news;server=(local)"  />
       </appsettings>
      </location>
      <location path="bbs">
       <appsettings>
        <add key="connstring" value="uid=flyangel;password=3.1415926;database=bbs;server=(local)" />
       </appsettings>
      </location>
      <location path="soft">
       <appsettings>
        <add key="connstring" value="uid=flyangel;password=3.1415926;database=soft;server=(local)" />
       </appsettings>
      </location>

      注:上例中news、bbs、soft分別是虛擬目錄下的子目錄。
      程序中使用連接時,采用下面的方法:
      public function getconnectionstring()
       configurationsettings.appsettings().item("connstring"
      end sub

      最后需要說明的一點是,為了有效地利用.config文件,你應當創(chuàng)建標準的鍵名和值定義供所有的應用程序開發(fā)人員所用。這樣就可以讓同一項目的開發(fā)人員采用公共的項目設置。這些標準在部署應用程序和將其轉(zhuǎn)化為產(chǎn)品的時候非常有用。


        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多