January 15, 2009

Extract Individual Contents of the Connection String from your Web.Config




A lot of blogs speak about retrieving ConnectionStrings from your web.config files. However what if we need to extract individual contents/portions like the DataSource or the Initial Catalog from the Connection String. This post explains how to extract them

C#


protected void Page_Load(object sender, EventArgs e)

{

string connStr = WebConfigurationManager.ConnectionStrings

    ["NorthwindConnectionString"].ConnectionString;

SqlConnectionStringBuilder bldr = 

    new SqlConnectionStringBuilder(connStr);

// displays (local)

string dataSrc = bldr.DataSource;

// displays Northwind

string iniCat = bldr.InitialCatalog;

}



VB.NET


    Protected Sub Page_Load(ByVal sender As Object, _

                            ByVal e As EventArgs)

        Dim connStr As String = _

        WebConfigurationManager.ConnectionStrings _

        ("NorthwindConnectionString").ConnectionString

        Dim bldr As New SqlConnectionStringBuilder(connStr)

        ' displays (local)

        Dim dataSrc As String = bldr.DataSource

        ' displays Northwind

        Dim iniCat As String = bldr.InitialCatalog

    End Sub



As shown above, we use the SqlConnectionStringBuilder to extract the contents of a connection string. This class provides a simple way to create and manage contents of the ConnectionString.

'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Extract Individual Contents of the Connection String from your Web.Config"
 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions