【问题标题】:using connectionstring from web.config instead of app.config使用来自 web.config 而不是 app.config 的连接字符串
【发布时间】:2011-06-24 03:11:21
【问题描述】:

我有一个类库项目,我在其中添加了一个与 sql server 对话的数据集。此项目的输出将从 Web 应用程序 project 中使用。所以我打算把我的连接字符串放在web应用项目中。

做了几件事。为了让我的适配器使用不同的连接字符串,我遇到了this。但我终于发现这样做很方便:

Dim adapter as New MyReqeustTableAdapter()
adapter.Connection.ConnectionString = sMyConnectionString

然后我尝试从我的配置 (app.config) 中获取连接字符串来模拟 .我使用键“myconstr”手动添加了一个部分。我的想法是做这样的事情:

sMyConnectionString = ConfigurationManager.ConnectionString("myconstr").ConnectionString

但我的智能感知无法检测到 ConfigurationManager。所以只好在项目中添加适当的引用。

接下来,我通过 Web 应用程序项目的设置设计器添加了一个连接字符串。我给出了上面的键来引用它。但是上面的语句似乎抛出了一个空引用异常。

【问题讨论】:

  • 您能否也发布一份 web.config 的副本。
  • 你能从你的 web.config 中显示“connectionStrings”部分吗?
  • 你不能提出问题,也不能与试图帮助你的人互动!

标签: asp.net .net connection-string tableadapter


【解决方案1】:

假设您已经创建了一个类库。您在其中定义了一个 Settings 属性,如下所示:

Properties.Settings.Default.ProjectName

Visual Studio 可能会自动为您生成一些配置,如下所示:

(app.config)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MyDllProject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <MyDllProject.Properties.Settings>
            <setting name="ProjectName" serializeAs="String">
                <value>MyDllproject</value>
            </setting>
        </MyDllProject.Properties.Settings>
    </applicationSettings>
</configuration>

现在假设您将此程序集添加到项目中。如果你访问它的设置,你很可能会得到MyDllproject 作为它的值。尽管添加了任何配置,但仍然如此。为什么?因为当程序集生成时,它是被写入其中的。并且编写的代码在没有配置覆盖的情况下使用生成时在 app.config 中定义的内容。

现在在您的目标项目中,您只需按照以下模式在配置文件中添加必要的部分

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>

        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <!-- start: copied from app.config of class library -->
            <section name="MyDllProject.Properties.Settings" 
                type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" 
                />
            <!-- end: copied from app.config of class library -->

            <!-- other sections may follow -->
        </sectionGroup>

    </configSections>

    <applicationSettings>
        <!-- remember to maintain the same order as how it was defined in the sectionGroup -->

        <!-- start: copied from app.config of class librarly -->
        <MyDllProject.Properties.Settings>
            <setting name="ProjectName" serializeAs="String">
                <value>ConsoleProjectB</value>
            </setting>
        </MyDllProject.Properties.Settings>
        <!-- end: copied from app.config of class library -->

        <!-- other configurations settings may follow -->
    </applicationSettings>
</configuration>

就是这样。

这是我链接到的一个小项目示例:http://sdrv.ms/16ksPef

【讨论】:

    【解决方案2】:

    根据您的问题,我认为您正在构建一个 n 层应用程序。我认为您最好选择您提到的两个选项。

    您应该为您的 DAL(数据访问层)类创建一个基类,该类将具有包含该连接字符串的公共属性。

    顺便说一句,它将帮助您隐藏/保护您的连接字符串,而不是将其存储在一个文件中,任何拥有或获得访问您主机的人都可以轻松读取的文件

     public class DALBase
     {
         public static string connString
         {
             get { return "Data Source=localhost\\SqlExpress;Initial Catalog=theDb Integrated Security=True"; }
         }
     }
    

    【讨论】:

    • 您正在硬编码连接字符串?
    【解决方案3】:

    基本上,您需要的是从外部更改库的全局参数的可能性。单独使用 app.config 并不能立即为您提供这种可能性——您还没有公开您的库内部设置。考虑到这一点,实现曝光的一种非常直接的方法是在您的库中为 app.config 创建一个分配器。

    public static class Setter
    {
        public static void Set(string name, string value)
        {
            Properties.Settings.Default[name] = value;
        }
    }
    

    然后,在 global.asax 或其他地方启动 web 应用程序:

    MyLibrary.Setter.Set("X", ConfigurationManager.ConnectionStrings["Y"].ConnectionString);
    

    【讨论】:

      【解决方案4】:

      类库项目中定义的配置文件(app.config)不能被系统自动使用。唯一可以使用的配置文件是 Web 应用程序中的 web.config 文件或 exe 应用程序中的 *myexe.exe*.config,其中 exe 文件为 *myexe.exe*

      所以您似乎正在尝试将app.config 添加到类库中。那是行不通的。

      可以将第二个配置文件链接到web.config,但这可能对您没有帮助。

      【讨论】:

        【解决方案5】:

        您可以添加对System.Web 的引用并使用类库项目中的System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString)

        【讨论】:

          猜你喜欢
          • 2015-02-24
          • 2017-01-20
          • 2013-03-04
          • 1970-01-01
          • 2011-02-18
          • 1970-01-01
          • 2011-06-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多