使用数据集向导添加数据集时如何引用app.config中的连接字符串?
每当我使用向导在类库项目中添加数据集时,它都会提供一个选项,将连接字符串保存在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
属性类型是应用程序和特定于用户的设置的包装,如此处。
您似乎正在尝试从库程序集 (.dll) 获取配置设置,而不是从引用您的库的应用程序/站点获取配置设置。我猜测这是基于您正在使用此属性的事实:
在引用的程序集需要来自正在运行的应用程序/站点的配置设置的情况下,我通常:
请注意,此静态类型需要在您的核心库之一中定义,因为您不能有循环引用: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:
In situations where a referenced assembly needs a configuration setting from the running application/site I usually:
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.