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

x
?
Solved

asp.net 4 get values from connection string

Posted on 2010-09-20
2
Medium Priority
?
452 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
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 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

972 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