Solved

asp.net 4 get values from connection string

Posted on 2010-09-20
2
448 Views
Last Modified: 2012-05-10
i have a sql connection string being stored in the web.config.

how can i pull items like server name, user name, password... inside of c# asp.net code?
0
Comment
Question by:realcoding
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 33721327
You might can get the connection string and then split it into an array like below:

using System.Configuration;

string s = ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString;
        string[] arr = s.Split(';');
        Dictionary<string, string> keypairs = new Dictionary<string, string>();
        foreach (string st in arr)
        {
            string[] kp = st.Split('=');
            if (kp.Length > 1)
            {
                keypairs.Add(kp[0], kp[1]);
            }
        }
       string servername = keypairs["Data Source"]; //access the items using the keys

Note: There should be an easy way but this was what I came up with
0
 
LVL 10

Accepted Solution

by:
james-ct16 earned 500 total points
ID: 33722044
Howdy

If you are working with SQL server you may find the SqlConnectionStringBuilder Class helpful.

The following is the msdn doco with example
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx

Hope that helps

James
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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 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