Solved

Get connection string from Settings.settings file

Posted on 2006-07-21
6
1,814 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:smickell
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 17154563
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"])
0
 

Author Comment

by:smickell
ID: 17154825
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.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17155113
1.1 or 2.0 ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:smickell
ID: 17155176
2.0 sorry I forgot to state this.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 100 total points
ID: 17155385
In that case, with a config something like:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>
        <add name="myConnString" connectionString="yadayada" />
      </connectionStrings>
    </configuration>

The you retrieve it using:

    string connStr = ConfigurationManager.ConnectionStrings["myConnString"].ConnectionString;

Don't forget to add a reference to the System.Configuration assembly and a "using" statement for the same.
0
 

Author Comment

by:smickell
ID: 17155587
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question