linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘网络学院网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > ASP >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·由HTTP 500 Internal server e
·会员系统“找回密码”的制作方
·ASP实现数字和字母组合并生成图
·简单的asp验证码程序示例
·用ASP制作强大的搜索引擎
·如何让图片自动缩放以适合界面
·如何用foreach遍历页面上所有的
·如何实现站点的RSS输出
·无组件实现文件上传/下载
·ASP如何获取真实IP地址
·asp中如何使用 HTML 表格
·采用XMLHTTP编写一个天气预报的
·80004005错误及其解决方法
·asp中如何调试 ASP 脚本
·asp+ajax打造无刷新新闻评论系
最新文章
·ASP教程:8、ASP内建对象Respo
·ASP教程:7、ASP内建对象Reque
·ASP教程:6、ASP脚本循环语句
·ASP教程:5、ASP脚本变量、函数
·ASP教程:4、ASP脚本基础
·ASP教程:3、ASP基础
·ASP教程:2、ASP简介
·ASP教程:1、Are you ready?
·ASP中使用SQL语句教程
·ASP进度条
·由HTTP 500 Internal server e
·Asp无组件生成缩略图
·ASP 系列函数大全(4)
·ASP 系列函数大全(3)
·ASP 系列函数大全(2)
Google
 
web应用程序中的数据库连接
[ 作者:  加入时间:2007-11-28 13:51:36  来自:Linux联盟收集整理 ]
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论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·php数据库连接  (2007-11-23 11:23:30)
 ·MySQL数据库连接过多的错误  (2007-11-16 09:49:39)
 ·perl 父子进程的数据库连接继承问题  (2007-11-09 10:42:39)
 ·用Perl 来创建可重用的Web应用程序  (2006-12-08 10:37:35)
 ·监控业务系统数据库连接  (2006-11-15 11:01:53)
 ·提高DB2 Web应用程序性能的五条规则  (2006-11-02 11:06:27)
 ·数据库连接过多的错误,可能的原因分析及解决办法  (2006-08-16 10:00:32)
 ·数据库连接的class  (2006-07-15 14:02:32)
 ·如何利用JBuilder2005开发Web应用程序  (2005-12-29 12:46:15)