locked
extract ConnectionString from App.Config RRS feed

  • Question

  • is there any way to extract ConnectionString values from App.Config so server will be put into txtServer, database into txtPassword, UID into txtUser and password into txtPassword?

    Jassim Rahma

    Thursday, April 12, 2012 6:12 PM

Answers

  • The SqlConnectionStringBuilder class will parse a connection string and provide you with properties corresponding to the key-value pairs in a typical connection string.

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx

    • Proposed as answer by Kris444 Friday, April 13, 2012 9:58 AM
    • Marked as answer by Mike Feng Saturday, April 21, 2012 7:39 AM
    Thursday, April 12, 2012 6:26 PM
  • Extending Eva's reply, you can apply the things as below,

    string connectionString = ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;
    SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder(connectionString);
    string dataSource = sb.DataSource;
    string userId = sb.UserID;
    string pwd = sb.Password;


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Kris444 Friday, April 13, 2012 9:58 AM
    • Marked as answer by Mike Feng Saturday, April 21, 2012 7:39 AM
    Friday, April 13, 2012 8:12 AM

All replies

  • The SqlConnectionStringBuilder class will parse a connection string and provide you with properties corresponding to the key-value pairs in a typical connection string.

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx

    • Proposed as answer by Kris444 Friday, April 13, 2012 9:58 AM
    • Marked as answer by Mike Feng Saturday, April 21, 2012 7:39 AM
    Thursday, April 12, 2012 6:26 PM
  • Extending Eva's reply, you can apply the things as below,

    string connectionString = ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;
    SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder(connectionString);
    string dataSource = sb.DataSource;
    string userId = sb.UserID;
    string pwd = sb.Password;


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Kris444 Friday, April 13, 2012 9:58 AM
    • Marked as answer by Mike Feng Saturday, April 21, 2012 7:39 AM
    Friday, April 13, 2012 8:12 AM