我们可以从asp.net Web应用程序中的“应用程序配置"中读取连接字符串吗? [英] Can we read connection string from Application config in asp.net web application

查看:74
本文介绍了我们可以从asp.net Web应用程序中的“应用程序配置"中读取连接字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从asp.net Web应用程序中的应用程序配置中读取连接字符串.如果是,怎么办?

Is it possible to read connection string from Application config in asp.net web application.If so How?

推荐答案

首先,在文件.
First, set the connection string in App.Config File.
<configuration>
  <connectionstrings>
    <add name="CharityManagement">
    connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
  </add></connectionstrings>
</configuration>


以您的形式;设置要使用的引用.


In your forms; set references that you want to use.

using System.Data.SqlClient;


然后,您可以使用ConnectionStrings属性从App.Config获取连接字符串.


Then you can get Connection String from App.Config by use ConnectionStrings properties.

var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;


参考: http://msdn.microsoft.com/en-us/library/ms178411.aspx [ ^ ]


Refer: http://msdn.microsoft.com/en-us/library/ms178411.aspx[^]


试试看..

Try this ..

<Configuration>
    <connectionStrings>
        <add name="AppName" connectionString="Connectionstring Name" />
    </connectionStrings>
</Configuration>





System.Configuration.ConfigurationManager.ConnectionStrings["AppName"].ToString();



这是详细的示例

http://www.dotnetfunda.com/articles/article283.aspx [ http://www.dreamincode.net/forums/topic/45321-grabbing-connectionstring -from-appconfig/ [ ^ ]

http://csharpdotnetfreak.blogspot.com/2012/07/webconfig-connectionstring-appsettings.html [^ ]

问候
Sebastian



Here is the detailed example

http://www.dotnetfunda.com/articles/article283.aspx[^]

http://www.dreamincode.net/forums/topic/45321-grabbing-connectionstring-from-appconfig/[^]

http://csharpdotnetfreak.blogspot.com/2012/07/webconfig-connectionstring-appsettings.html[^]

Regards
Sebastian


是的,您可以这样做

在web.config中:

Yes, you can do like this

in web.config:

<Configuration>
    <connectionStrings>
        <add name="ConnectionString" connectionString="Connectionstring Name" />
    </connectionStrings>
</Configuration>




您的解决方案页面:




your solution page:

Dim ConStr As String = ConfigurationSettings.AppSettings("ConnectionString")


这篇关于我们可以从asp.net Web应用程序中的“应用程序配置"中读取连接字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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