Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get connection string from Settings.settings file

Posted on 2006-07-21
6
Medium Priority
?
1,836 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 400 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses

926 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