|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
|
Web.config文件是标准的XML文件,我们可以使用它来为一台机器下的每一个web应用程序或某个应用程序或一个目录下的ASP.net页面来进行设置,当然,它也可以为一个单独的web页面进行设置。 3htLinux联盟 如:网站的主目录是\inetpub\wwwroot\,那么我们将web.config放置于其下,那么这个网站中的应用程序将被web.config中的设置所影响。 3htLinux联盟 e.g.: 3htLinux联盟 <?xml version="1.0" encoding="gb2312" ?> 3htLinux联盟 <configuration> 3htLinux联盟 <system.web> 3htLinux联盟 <compilation defaultlanguage="VB" debug="true" /> 3htLinux联盟 <customerrors mode="remoteonly" defaultredirect="js/error.htm"> 3htLinux联盟 <error statuscode="404" redirect="js/filenotfound.aspx" /> 3htLinux联盟 <error statuscode="500" redirect="js/error.htm" /> 3htLinux联盟 </customerrors> 3htLinux联盟 <authentication mode="Windows" /> 3htLinux联盟 <authorization> 3htLinux联盟 <allow users="*" /> 3htLinux联盟 </authorization> 3htLinux联盟 <httpruntime maxrequestlength="4000" usefullyqualifiedredirecturl="true" executiontimeout="45" /> 3htLinux联盟 <trace enabled="false" requestlimit="10" pageoutput="false" tracemode="sortbytime" localonly="true" /> 3htLinux联盟 <sessionstate mode="inproc" stateconnectionstring="tcpip=127.0.0.1:43444" cookieless="false" timeout="20" /> 3htLinux联盟 <globalization requestencoding="gb2312" responseencoding="gb2312" fileencoding="gb2312" /> 3htLinux联盟 </system.web> 3htLinux联盟 <appsettings> 3htLinux联盟 <add key="connstring" value="uid=Flash;password=3.1415926;database=news;server=(local)" /> 3htLinux联盟 </appsettings> 3htLinux联盟 </configuration> 3htLinux联盟 3htLinux联盟 这里我们讨论一下如何在web.config中设置数据库连接。 3htLinux联盟 3htLinux联盟 1、连接一个数据库: 3htLinux联盟 在web.config中的<configuration>后加入 3htLinux联盟 3htLinux联盟 <appsettings> 3htLinux联盟 <add key="connstring" 3htLinux联盟 value="uid=flash;password=3.1415926;database=news;server=(local)" /> 3htLinux联盟 </appsettings> 3htLinux联盟 3htLinux联盟 在程序中,你可以使用以下代码来使用web.config中的设置: 3htLinux联盟 3htLinux联盟 -----VB.net----- 3htLinux联盟 imports system.configuration 3htLinux联盟 dim myvar as string 3htLinux联盟 myvar=configurationsettings.appsettings("connstring") 3htLinux联盟 -----C#----- 3htLinux联盟 using system.configuration; 3htLinux联盟 string myvar; 3htLinux联盟 myvar=configurationsettings.appsettings["connstring"]; 3htLinux联盟 3htLinux联盟 2、连接多个数据库 3htLinux联盟 同理,那就是使用多个不同的key值来设置 3htLinux联盟 三 3htLinux联盟 VB:HTML与TEXT的转换 VC:VC++任务栏提示区图标的实现 3htLinux联盟 3htLinux联盟 3htLinux联盟 3、设置不同子目录下应用程序的数据库链接 3htLinux联盟 这是一个很有意思的方法,在设置前,先说明一下它的用途: 3htLinux联盟 如果在一个虚拟目录下有多个子目录,每一个子目录下下的Web应用程序都需要连接不同的数据库,这如何做呢?? 3htLinux联盟 一种方法是在每一个子目录下分别建立一个web.config,用它来设置这个目录下的数据库连接。但这种方法的问题是需要维护每一个了目录下的web.config。 3htLinux联盟 3htLinux联盟 方法二,是只在虚拟目录下建立一个web.config,在它里面设置每一个子目录下的应用程序的数据库连接。说到这里,你会想到上面的第二种方法,使用多个不同的key值来设置,这的确是一个办法。 3htLinux联盟 3htLinux联盟 这里,我想说明的是另一种方法:在虚拟目录下布置web.config,在其中使用location标记,使用同一个key值来连接数据库,这样做的好处很明显,因为用同一个key值,将导致在所有目录下的应用程序中,都可以使用共同的语句来连接数据库,这在程序以后发生位置迁移时,并不用修改程序中连接数据库的语句。 3htLinux联盟 具体设置如下: 3htLinux联盟 3htLinux联盟 <location path="news"> 3htLinux联盟 <appsettings> 3htLinux联盟 <add key="connstring" value="uid=flyangel;password=3.1415926;database=news;server=(local)" /> 3htLinux联盟 </appsettings> 3htLinux联盟 </location> 3htLinux联盟 <location path="bbs"> 3htLinux联盟 <appsettings> 3htLinux联盟 <add key="connstring" value="uid=flyangel;password=3.1415926;database=bbs;server=(local)" /> 3htLinux联盟 </appsettings> 3htLinux联盟 </location> 3htLinux联盟 <location path="soft"> 3htLinux联盟 <appsettings> 3htLinux联盟 <add key="connstring" value="uid=flyangel;password=3.1415926;database=soft;server=(local)" /> 3htLinux联盟 </appsettings> 3htLinux联盟 </location> 3htLinux联盟 3htLinux联盟 注:上例中news、bbs、soft分别是虚拟目录下的子目录。 3htLinux联盟 程序中使用连接时,采用下面的方法: 3htLinux联盟 public function getconnectionstring() 3htLinux联盟 configurationsettings.appsettings().item("connstring") 3htLinux联盟 end sub 3htLinux联盟 3htLinux联盟 最后需要说明的一点是,为了有效地利用.config文件,你应当创建标准的键名和值定义供所有的应用程序开发人员所用。这样就可以让同一项目的开发人员采用公共的项目设置。这些标准在部署应用程序和将其转化为产品的时候非常有用。 3htLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|