Web.Config 中的连接字符串存储在哪里?
我们可以通过两种方式在 Web.config 文件中存储连接字符串
一种是
<connectionStrings>
<clear/>
<add name="LocalSqlServer"
connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
另一种是
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=Northwind;uid=sa;password=secret;" />
</appSettings>
现在我想知道
这两种方法有什么区别?
哪一种方法更好?
它们有什么局限性?
更新:您能否解释一下
与
相比有何显着优势?
We can store Connection String in Web.config file in two ways
One is
<connectionStrings>
<clear/>
<add name="LocalSqlServer"
connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Other One is
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=Northwind;uid=sa;password=secret;" />
</appSettings>
Now I want to know
What is difference between these two approach?
Which one is better way?
What are their limitations?
UPDATE:Can you explain that <connectionString>
has any significant advantage over <appSetting>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
连接字符串部分是声明系统的连接。我的意思是您的应用程序知道您的字符串是连接字符串。如果您将 appSettings 用于应用程序,则它只是任何字符串值。
connection-string section is declare the connection for system.I mean that you application know that you string is connectionString. If you will use appSettings for application it is just any string value.
除了其他答案中提到的好处之外,
connectionStrings
元素还具有providerName
属性,而appSettings
元素没有。如果您的数据源不是 SQL Server,这尤其有用。Besides the benefits mentioned in the other answers,
connectionStrings
elements have aproviderName
attribute thatappSettings
elements do not. This is particularly useful if your data source is not SQL Server.connectionStrings
部分专用连接字符串,仅在 .NET 2.0 中引入。appSettings
更为通用,用于其他应用程序设置。您应该使用
connectionStrings
部分,因为它也可以加密与任何其他设置分开。The
connectionStrings
section is dedicated to connection strings and was only introduced in .NET 2.0.appSettings
is more general and is to be used for other application settings.You should use the
connectionStrings
section, as it can also be encrypted separately from any other settings.第一种方法可以通过一些数据控件(例如 SQLDataSource)直接访问。
The first approach can be accessed directly by some data controls like SQLDataSource.
使用 connectionStrings 元素将是处理连接字符串的最合适方法。 appSettings 元素是 .NET 2.0 之前处理连接字符串的方式。您可以使用任一方法,但如果使用 connectionString 元素,则使用多个连接字符串可能会更容易。对于存储在 appSettings 中的多个连接字符串,您必须先解析每个名称(或值)以确定它是否是连接字符串,然后才能使用它。这会导致维护问题。仅检查所有的connectionString 项是否都存在会更容易。
Using the connectionStrings element would be the most appropriate way to handle connection strings. The appSettings element is how connection strings used to be handled before .NET 2.0. You can use either approach but it is probably easier to work with multiple connection strings if you use the connectionString element. With multiple connection strings stored in appSettings you would have to parse each name (or value) to work out if it is a connection string before you could use it. This leads to maintenance issues. It's easier to just check if all of the connectionString items are present.
如果将连接字符串添加到 appSettings 部分,则需要使用 ConfigurationManager.AppSettings.Get(key) 方法。
通过将连接字符串添加到 connectionStrings 元素,.NET 可以自动创建连接对象时按名称查找它们。
If you add your connection strings to the appSettings section, you need to manually retrieve them using the ConfigurationManager.AppSettings.Get(key) method.
By adding your connection strings instead to the connectionStrings element, .NET can automatically find these by name when you create your connection object.