连接字符串和应用程序设置之间有什么区别? [英] What is the difference between connectionstrings and appsettings?

查看:70
本文介绍了连接字符串和应用程序设置之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我所指的其中一个应用程序中,连接字符串存储在AppSettings中!到目前为止,我一直将连接存储在<connectionstring/>元素中.但是正确的方法是什么?

In one of the applications I have been referring to, the Connection String is stored in AppSettings! Till now I have been storing the connection in the <connectionstring/> element. But what is the correct way?

所以我的问题是,web.config中的<connectionstrings><appsettings>之间有什么区别?是否有任何特定的原因为什么我应该或不应该在应用程序设置中存储连接字符串?是否提供任何要遵循的规则/准则?还是这完全是开发人员的选择?

So my question is, what are the differences between <connectionstrings> and <appsettings> in the web.config and are there any specific reasons why I should or should not be storing connection strings in appsettings? Are there any rules / guidelines provided to follow? Or is this completely the choice of the developer?

推荐答案

connectionStringappSettings之间存在根本区别:

There is a fundamental difference between connectionString and appSettings:

他们寻找不同的东西.在.NET 2.0及更高版本中:

They look for different things. In .NET 2.0 and above:

connectionString对象是一个XML节点,具有要设置的特定属性.在语义上是指数据库连接字符串.

A connectionString object is an XML node that has specific attributes to set; and semantically it refers to a database connection string.

例如,connectionString如下所示:

<connectionStrings>
    <clear/>
    <add name="LocalSqlServer"
          connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True"
          providerName="System.Data.SqlClient" />
  </connectionStrings>

您会注意到它具有一些不同的属性:

You'll notice it has a few different attributes:

  • name
  • connectionString:其中有一个特定的字符串,它需要一个Initial Catalog安全机制(在本例中为Integrated Security
  • providerName
  • name
  • connectionString : This has a specific string inside of it, it needs an Initial Catalog, a security mechanism (in this case Integrated Security
  • providerName

appSettings只是用户定义的键/值对,它允许您...很好...设置应用程序设置.可以是任何东西:

Whereas appSettings is just a user-defined Key-value pair that allows you to... well... set application settings. It can be anything:

<appSettings>
    <add key="Mom" value="Your"/>
    <add key="UseCache" value="True"/>
    <add key="MapsKey" value="1234567890-AA"/>
    <add key="SMTPServer" value="smtp.peterkellner.net"/>
</appSettings>

在许多情况下,将connectionString放在像appSettings这样的键值对中(通过编程方式)是 odd .以及的难度也越来越大.

In many cases, it would just be odd to put the connectionString in a key-value pair like appSettings (semantically and programmatically). As well as it would make it more difficult to encrypt the connectionString when you need to.

此博客文章中有关于此的更多信息.

There is more information about this from this blog post.

这篇关于连接字符串和应用程序设置之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆