我需要获取有关ASP.NET中ConfigurationManager的详细信息 [英] I required to get details about ConfigurationManager in ASP.NET

查看:99
本文介绍了我需要获取有关ASP.NET中ConfigurationManager的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


有什么区别

What is the difference between

public SqlConnection getConnection()
   {SqlConnection conn;
       string str = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
       conn = new SqlConnection(str);
       conn.Open();
       return conn;
   }





public SqlConnection getConnection()
    {
        string str = "Data source=Gisserver;initial catalog=Database;Uid=sa;pwd=sa";
        conn = new SqlConnection(str);
        conn.Open();
        return conn;
    }



我使用第二个然后建立连接.为什么不首先正确执行.



I use second one then connection established . Why not executed first properly.

推荐答案

第一和第二代码段之间的区别是:

在第一种情况下,您将连接字符串存储在配置文件中.
优点是,它没有在您的应用程序中进行硬编码,因此可以轻松配置.我的意思是,您可能会遇到需要更改连接字符串的情况,使用此选项,您可以轻松更改服务器名称,数据库或身份验证信息,而无需编辑单个Web页面.此外,您可以使用加密保护连接字符串
在第二种情况下,您正在对连接字符串进行硬编码,由于我上面解释的原因,这不是一个好习惯.

其他响应者已经告诉您为什么第一种情况不起作用. :)
The difference between the 1st and 2nd code snippet is:

In the first case, you are storing the connection string in a config file.
The advantage being, it is not hard coded in your application and so it can be easily configured. What I mean is you may have a situation where you need to change your connection string, with this option you can easily change the server name, database, or authentication information without editing individual Web pages. Additionally, you can secure the connection string using encryption
In second case, you are hard coding the connection string which is not a good practice for the reason I explained above.

The other responders have already told you why the first case is not working. :)


我认为您的第一个代码没有问题,您应该检查是否在Web配置文件中正确添加了连接字符串.
这是我认为的唯一问题.
I don''t think there is a problem in your code for first one you should check if you add connection string properly in you web config file.
This is the only one problem i think in it.


如果是第一个问题,即
ConfigurationManager.ConnectionStrings ["MyConnection"].ConnectionString
您必须像
一样将连接字符串放在web.config中
in case of the first one i.e
ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString
u have to put your connection string in web.config like
<appSettings>
  <add key="MyConnection" value="Data Source=RITESH\SQLEXPRESS;Initial Catalog=Test1; Integrated Security=true"/>
</appSettings>



及其在网络配置中保留连接字符串的安全方法.
以这种方式连接不会有任何问题.

================================================== ================
阿伦:我认为这是不正确的.您指定的是ConfigurationManager.AppSettings["MyConnection"]而不是ConfigurationManager.ConnectionStrings["MyConnection"].
================================================== ===============



and its secure way to keep connection string in web config.
there should not be any issue og not getting connection this way.

==================================================================
Arun : I don''t think that this is correct.What you specified is ConfigurationManager.AppSettings["MyConnection"] not ConfigurationManager.ConnectionStrings["MyConnection"].
==================================================================


这篇关于我需要获取有关ASP.NET中ConfigurationManager的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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