Solved

asp.net 4 get values from connection string

Posted on 2010-09-20
2
442 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 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

20 Experts available now in Live!

Get 1:1 Help Now