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 = n开发者_如何学Cew 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?
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:
- Create an AppNameApplication static type in a core library (that is, an assembly with few or no other custom dependencies)
- Create properties for either the configuration as a whole (if using a custom ConfigurationSection) or for each setting needed.
- Initialize this static class at the start of the application (main or Global.Applicaton_Start)
- 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.
精彩评论