使用数据集向导添加数据集时如何引用app.config中的连接字符串?

发布于 2024-12-01 01:02:19 字数 600 浏览 3 评论 0原文

每当我使用向导在类库项目中添加数据集时,它都会提供一个选项,将连接字符串保存在 app.config 文件中,选择该选项后,它会将字符串保存在文件中,但当我检查数据集设计器时,它总是保存它在项目属性对象中:

private void InitConnection() {
            this._connection = new global::System.Data.SqlClient.SqlConnection();
            this._connection.ConnectionString = global::BaseClassLibrary.Properties.Settings.Default.DBConnectionString;
        }

这不是很有用,因为当我尝试使用此项目 dll 并通过在 web.config 或 app.config 中写入它来覆盖连接字符串时...它没有引用它...

并且一个有趣的事实是,如果您遵循在 Web 项目中使用向导添加数据集的相同过程,那么它实际上引用 web.config 作为连接字符串...这有点奇怪...并且在 Web 项目数据集中不生成设计器类...

我是否可以执行所需的操作?

Whenever I add dataset in my class library project using the wizard it gives me an option to save the connection string in app.config file and after selecting the option it do save the string in file but when I check the dataset designer it always saves it in project property object:

private void InitConnection() {
            this._connection = new global::System.Data.SqlClient.SqlConnection();
            this._connection.ConnectionString = global::BaseClassLibrary.Properties.Settings.Default.DBConnectionString;
        }

and this is not so useful because when I try to use this project dll and override the connection string by writing it in web.config or app.config ... it doesn't refer to it ...

and one interesting fact that if you follow the same process of adding dataset using wizard in web project then it actually refer web.config for connection string ... which is a bit strange ... and in web project dataset don't generate designer classes ...

Is there anyway I can do the desire action?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半葬歌 2024-12-08 01:02:19

属性类型是应用程序和特定于用户的设置的包装,如此处。

您似乎正在尝试从库程序集 (.dll) 获取配置设置,而不是从引用您的库的应用程序/站点获取配置设置。我猜测这是基于您正在使用此属性的事实:

global::BaseClassLibrary.Properties.Settings.Default.DBConnectionString;

在引用的程序集需要来自正在运行的应用程序/站点的配置设置的情况下,我通常:

  1. 在核心库中创建 AppNameApplication 静态类型(即,具有很少或没有其他自定义依赖项)
  2. 为整个配置(如果使用自定义 ConfigurationSection)或所需的每个设置创建属性。
  3. 在应用程序启动时初始化此静态类(main 或 Global.Applicaton_Start)
  4. 从库程序集类型中引用 AppNameApplication 以访问这些配置设置。

请注意,此静态类型需要在您的核心库之一中定义,因为您不能有循环引用:App - Library - App。

希望这有帮助。

The Properties type is a wrapper around application and user-specific settings as described here.

It appears that you are attempting to get the configuration settings from your library assembly (.dll) rather than the configuration settings from the application/site that is referencing your library. I am guessing this based on the fact that you are using this property:

global::BaseClassLibrary.Properties.Settings.Default.DBConnectionString;

In situations where a referenced assembly needs a configuration setting from the running application/site I usually:

  1. Create an AppNameApplication static type in a core library (that is, an assembly with few or no other custom dependencies)
  2. Create properties for either the configuration as a whole (if using a custom ConfigurationSection) or for each setting needed.
  3. Initialize this static class at the start of the application (main or Global.Applicaton_Start)
  4. Reference AppNameApplication from the library assembly types to get access to these configuration settings.

Note that this static type needs to be defined in one of your core libraries because you cannot have a circular reference: App - Library - App.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文