Solved

Get connection string from Settings.settings file

Posted on 2006-07-21
6
1,780 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now