Link to home
Start Free TrialLog in
Avatar of smickell
smickellFlag for United Kingdom of Great Britain and Northern Ireland

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.[something?])

The name of the connection string I want is dbJobRegisterConnectionString.
Also, however it is done, can I access it in a tightly typed fashion, rather than having to type something.something["dbJobRegisterConnectionString"] if you know what I mean?
Thanks in advance.
Avatar of mrichmon
mrichmon

Well, if you put it in a .config file then you can have in your web.config this:

<appSettings configSource="Settings.config" />

Then have the Settings.config look like:
<?xml version="1.0"?>
<appSettings>
 <add key="dbJobRegisterConnectionString" value="your connection string here" />
</appSettings>

Then to access you simply do:

WebConfigurationManager.AppSettings["dbJobRegisterConnectionString"]

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(WebConfigurationManager.AppSettings["myDate"])
Avatar of smickell

ASKER

The appropriate section in my app.config is:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<connectionStrings>
        <add name="JobRegister.Properties.Settings.dbjobregisterConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbjobregister.mdf;Integrated 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.
Avatar of Carl Tawn
1.1 or 2.0 ?
2.0 sorry I forgot to state this.
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.Settings.dbjobregisterConnectionString" so I had to type that whole string in to make it work. I just snipped the "JobRegister.Properties.Settings" 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(ConfigurationManager.ConnectionStrings["dbjobregisterConnectionString"].ConnectionString);
sqlconn.Open();
SqlCommand sqlcmd = new SqlCommand(sqlstr, sqlconn);

Thanks again.