Solved

Get connection string from Settings.settings file

Posted on 2006-07-21
6
1,792 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

18 Experts available now in Live!

Get 1:1 Help Now