smickell
asked on
Get connection string from Settings.settings file
I have 3 connection strings stored in my Settings.settings file and I would like to refer to one of them from code rather than re-typing the string.
The code is:
SqlCommand sqlcmd = new SqlCommand(mySqlString, System.Configuration.[some thing?])
The name of the connection string I want is dbJobRegisterConnectionStr ing.
Also, however it is done, can I access it in a tightly typed fashion, rather than having to type something.something["dbJob RegisterCo nnectionSt ring"] if you know what I mean?
Thanks in advance.
The code is:
SqlCommand sqlcmd = new SqlCommand(mySqlString, System.Configuration.[some
The name of the connection string I want is dbJobRegisterConnectionStr
Also, however it is done, can I access it in a tightly typed fashion, rather than having to type something.something["dbJob
Thanks in advance.
ASKER
The appropriate section in my app.config is:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="JobRegister.Properti es.Setting s.dbjobreg isterConne ctionStrin g"
connectionString="Data Source=.\SQLEXPRESS;Attach DbFilename =|DataDire ctory|\dbj obregister .mdf;Integ rated Security=True;Connect Timeout=30;User Instance=False"
providerName="System.Data. SqlClient" />
...(other connection strings)....
</connectionStrings>
<applicationSettings>
....other settings...
</applicationSettings>
</configuration>
This is a windows application so I don't have WebConfigurationManager and I can't find the equivalent class.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="JobRegister.Properti
connectionString="Data Source=.\SQLEXPRESS;Attach
providerName="System.Data.
...(other connection strings)....
</connectionStrings>
<applicationSettings>
....other settings...
</applicationSettings>
</configuration>
This is a windows application so I don't have WebConfigurationManager and I can't find the equivalent class.
1.1 or 2.0 ?
ASKER
2.0 sorry I forgot to state this.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
A faultless solution there carl_tawn, thanks :)
I only had one problem and that was because I built the connection string from a VS wizard - it set the name of the connection string to be "JobRegister.Properties.Se ttings.dbj obregister Connection String" so I had to type that whole string in to make it work. I just snipped the "JobRegister.Properties.Se ttings" from the app.config and it still works the same. Why that is needed I do not know.
For others wishing to use this solution, the code can be adapted from:
SqlConnection sqlconn = new SqlConnection(Configuratio nManager.C onnectionS trings["db jobregiste rConnectio nString"]. Connection String);
sqlconn.Open();
SqlCommand sqlcmd = new SqlCommand(sqlstr, sqlconn);
Thanks again.
I only had one problem and that was because I built the connection string from a VS wizard - it set the name of the connection string to be "JobRegister.Properties.Se
For others wishing to use this solution, the code can be adapted from:
SqlConnection sqlconn = new SqlConnection(Configuratio
sqlconn.Open();
SqlCommand sqlcmd = new SqlCommand(sqlstr, sqlconn);
Thanks again.
<appSettings configSource="Settings.con
Then have the Settings.config look like:
<?xml version="1.0"?>
<appSettings>
<add key="dbJobRegisterConnecti
</appSettings>
Then to access you simply do:
WebConfigurationManager.Ap
which returns a string by default so in your case it is already typed.
If it were a date then you would do:
Convert.ToDateTime(WebConf