Assuming that storing connection string in a config file is more secure than compiled code,
so I wonder a way to compile only the database name since I have to change it frequently.
I am working with sql server and Entity Framework in windows Forms.
Thanks for your inputs.
Loading

brunda kRank #3,804 · 389 reputationPosted May 30, 2012, 10:22 PM EDT
I think you got a better solution,SqlConnectionStringBuilder.
hope by writing system.config.dll means you are manually adding reference to system.configuration.dll in VS.
ConfigurationSettings is an obsolete method, in the link which you have provided
ConnectionStringSettings settings =ConfigurationManager.ConnectionStrings[name];
Thanks!
brunda kRank #3,804 · 389 reputationPosted May 29, 2012, 9:38 PM EDT
Samio170 reputationPosted May 30, 2012, 12:35 AM EDT
The provided link shows how to build the entire connection string ( may include sensitive data) at runtime and then how to store it in a config file. The required thing is how to keep sensitive data in app.config and only pass the database name as parameter from code.
brunda kRank #3,804 · 389 reputationPosted May 30, 2012, 1:50 AM EDT
In app.config
please include all the data which is to be hardcoded except the database name which is to send thru code dynamically.
Using above URL code build and append only code for database in
StringBuilder Con.
Con.Append(";Initial Catalog=");
Con.Append(DatabaseName);
Samio170 reputationPosted May 30, 2012, 2:00 AM EDT
This is a good advice, thank you.
I'll try this solution in a while and then give you a feed back. Remain then to think about it in point of view security.
Samio170 reputationPosted May 30, 2012, 7:39 AM EDT
//to refresh connection string each time else it will use previous connection string
ConfigurationManager.RefreshSection("connectionStrings");
config file size is growing because cannot clear the previous added value.
Also as advised at my first post I am using an EF connection type, any idea please on how to make default entities within the model use the new connection ?
brunda kRank #3,804 · 389 reputationPosted May 30, 2012, 12:01 PM EDT
1.For EF requirment, the above link provides sample to update the connectionstring in app.config.
2. not clearing previous values for this query we are not actually adding but updating
public void updateConfigFile(string con) , so the size of config file should be same
Samio170 reputationPosted May 30, 2012, 1:45 PM EDT
Thanks a lot for your valuable help,
- [not clearing previous values] I've seen that happening in the MyApplication\bin\debug\MyApplication.exe.config file each time I connected to database.
- [EF Connection] You provided a very good link on that matter, but pending to try it, I am currently testing this one:
http://msdn.microsoft.com/en-us/library/ms254947.aspx
The result is that it worked like a charm in a new project, but when transferred to my current project and in spite system.config.dll is referenced in the project, I got red underline on the word "ConfigurationManager" in:
"ConfigurationSettings settings = ConfigurationManager.ConnectionStrings["partialConnectString"];"
and I cannot get ConfigurationManager under System.Configuration (System.Configuration.ConfigurationManager)
Samio170 reputationPosted May 31, 2012, 12:42 AM EDT
ConnectionStringSettings settings =ConfigurationManager.ConnectionStrings[name]; is the right choice
Many Thanks brunda k, it's finally done.
To make this topic more helpful please rename your posts titles as I did:
Re: How to set parameter into connection string in config file at runtime