频道直达 - 学院 - 下载 - 交易 - 特效 - 字库 - 手册 -排名-工具- 繁體
网页教学网站开发 设为首页
加入收藏
联系我们
建站搜索: 常用广告代码   用户注册 | 用户登陆
您当前的位置:中国建站之家 -> 网站开发设计技术教程 -> Asp.Net教程 -> ASP.NET: Connection Strings

ASP.NET: Connection Strings

作者:未知  来源:转载  发布时间:2005-7-20 9:58:03  发布人:acx

减小字体 增大字体

By Steven Smith from aspalliance.com


In ASP.NET there are several options for storing connection strings. These include the Web.config file, Application variables, custom text files, and hardcoded into pages or components. In Classic ASP, it was common practice to use the Application object to store the connection string information for an application, so it was common to find code like this in the global.asa file:

Sub Application_OnStart
        Application("Conn1_ConnectionString") = _
        "Provider=SQLOLEDB.1;UID=sa;PWD=p;Initial Catalog=src;Data Source=localhost;"
End Sub

This was the "best practice" approach for many websites. Another option that some sites, such as ASPAlliance.com, used was to have these settings in a common text file that was then called with Server.Execute, such as this:
Sub Application_OnStart
        Server.Execute "/db/config.asp"
End Sub

This would allow many different applications (like all of the individual columnists on ASPAlliance.com) to have their connection information stored in one place. ASPAlliance also used a COM object's property for this same purpose for a while. The question is, what's the best way to store this information in ASP.NET?

Well, there is no single right answer for this question, but depending on your situation, there is usually a method that is best. Before we consider the different possibilities, let's consider one option that should probably be eliminated right away: Application variables.

Don't Use Application Variables for Sensitive Information in ASP.NET
Although this worked wonderfully well in ASP, there is one thing new about ASP.NET that makes using the Application (or Session) object for storing database connection information unwise: Tracing. In a typical ASP application, it was fairly rare to find a page that would dump out the contents of the Application collection. If such a page existed, usually only the developer of the site knew about it. So having sensitive information in the Application object wasn't terribly insecure, as long as you were careful not to give the users any easy way to get a look at this collection of values. However, in ASP.NET, a trace dump will always show the contents of the Application collection. And anybody who knows anything about ASP.NET will know how to hit the Trace.axd file to see recent Trace dumps. Now, you say, "But you can configure tracing in the Web.config file, and I'll just turn it off there and be secure." Certainly, you should do that. However, don't forget that individual pages can have tracing enabled as well, and there is no global way to turn this off. And this is a GREAT debugging tool, so it's probably going to be used quite a bit by developers. That said, how long do you think it will be before a page slips by and is published with tracing still turned on? As soon as it does, your connection information will be publicized to the world if it is stored in your Application (or Session) object. I strongly believe that this will happen with relative frequency, and that for this reason alone it is a BAD IDEA to store sensitive data in the Application (or Session) object in ASP.NET.

Ok, so where DO I store this stuff?
Well, let's look at the most common situation -- the single web site. If you're building a single web site, and it has a single database supporting it, then your best bet is to follow the examples in place at IBuySpy.com and store your connection information in your Web.config file. It is then very easy to retrieve this information from any of your ASP.NET pages, using the following syntax:

        'VB
        Dim connstring as string
        connstring = ConfigurationSettings.AppSettings("ConnectionString")
       
        //C#
        string connstring = ConfigurationSettings.AppSettings["ConnectionString"];

To add support for this to your Web.Config, you need to add an AppSettings section, like this one:
<appSettings>
        <add key="ConnectionString" value="(your connection string)" />
</appSettings>

which should go right after the "configuration" section and before the "system.web" section. These settings can also be read from your custom data access components, as the IBuySpy examples also demonstrate. This is the recommended method for storing database connection information for small applications.

For larger appliations, such as the ASPAlliance.com website, which has dozens of different web applications all relying on the same database, there are several options. Since all of the columnist websites are subwebs of the root site, the easiest thing to do is to store the global config information in the root web's Web.config file. This is what I've done for ASPAlliance.com. For a server that has many different sites all using the same database (i.e. different IP addresses and domain names, not subwebs), the best solution if you want to keep all of the connection information in one place is to use the machine.config file for the server. There is an appSettings section already in the machine.config file that serves this purpose. Another option is to build a shared control, and have all of the sites use this, but this is less intuitive to use than the web.config method, and would require some instruction to new columnists whenever they needed to connect. Whenever possible, the web.config file should be used for storing sensitive application information, and remember that all sites inherit from the machine.config, and subwebs inherit from parent webs, so store your connection info as high up in the tree hierarchy as necessary to allow all the sites that need it to access it.
       

将本文收藏到QQ书签与更多好友分享
[打 印]
[] [返回上一页] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
精彩推荐
热门文章
· 注册码大全二
· 注册码大全四
· 注册码大全一
· 要10G免费网络硬盘的请进..
· 通过google 赶快来赚美金..
· 注册码大全十
· 头像-qq头像(qq新头像)4..
· 让你轻松架设FTP服务器1..
· 注册码大全三
· 梦幻背景图片7
· 卡通动物图片6
· 网页制作素材-按钮素材2..
· 让你轻松架设FTP服务器5..
· 风景图片8
· 注册码大全九
· 让你轻松架设FTP服务器2..
关注此文读者还看过
· 初级物理模型的构建(AS..
· JSP中的TagLib应用(4-3)..
· 用Photoshop轻松打造透明..
· 走近asp.net
· 用tar包配置高可用性vsf..
· 浅谈ASP中Web页面间的数..
· 搜索引擎市场调查:谷歌市..
· 如何选购虚拟主机?
· 陈一舟:最正确是学会撤..
· 德国人当上新浪第二大股..
· 平面软件 Photoshop 发展..
· AJAX开发应用入门
· 在ASP中使用SQL语句之2:..
· 网银存款丢失惹数百储户..
· php5学习笔记(转)
· 使用纯粹的asp+语言制作..
相关文章
· ASP.NET连接Access和SQL Se..
· Asp.net中防止用户多次登录..
· ASP.NET 与 Ajax 的实现方式..
· ASP.net平台社区软件Discuz..
· ASP.NET AJAX中的异步..
· 解析:如何在 ASP.NET 中下..
· ASP.Net中保护自定义的服务..
· ASP.NET中的doPostBack脚本..
· 不使用VS进行ASP.NET Membe..
· 关于ASP.NET编程中的嵌套If..
· ASP.NET 2.0 页面状态持续程..
· ASP.NET中的日期与时间的处..
· Asp.net编程中的数组基础实..
· 简单介绍 ASP.NET 中的运算..
· Asp.Net 构架(Http请..
· Asp.net中的Popup控件..
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 人才招聘
网站合作、内容监督、商务咨询:QQ: 9576619
Copyright ? 2005--2008 中国建站之家版权所有
粤ICP备05092265号