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 kPosted May 31, 2012, 2:22 AM
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!
SamioPosted May 31, 2012, 4:42 AM
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
SamioPosted May 30, 2012, 5:45 PM
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)
brunda kPosted May 30, 2012, 4:01 PM
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
SamioPosted May 30, 2012, 11:39 AM
//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 ?
SamioPosted May 30, 2012, 6:00 AM
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.
brunda kPosted May 30, 2012, 5:50 AM
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);
SamioPosted May 30, 2012, 4:35 AM
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 kPosted May 30, 2012, 1:38 AM