?
Solved

asp.net 4 get values from connection string

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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