How can I parse my custom connection string?

I am using my own custom connection string in web.config file to connect to smtp server to send emails in my web application how can I parse connection string to get server port user etc.

Download Code | Run Sample

DbConnectionStringBuilder class available in System.Data.Common Namespace provides the base class from which the strongly typed connection string builders. This class is can be very useful to parse your own connection string.

Let's take example of connection string that you might need for connecting with SMTP server. Information that you need to connect to SMTP server are.

  1. Server address
  2. Server Port
  3. User Name
  4. Pass word

If you put all things together then your connection string will look somewhat like.

"server=smtp.gmail.com; port=587; user=you@gmail.com; password=yourpassword"

And code to parse your connection string will look like following.

DbConnectionStringBuilder builder 
                = new DbConnectionStringBuilder();
builder.ConnectionString
                = @"server=smtp.gmail.com; port=587;
                user=you@gmail.com; password=yourpassword";

string server = builder["server"];
string port = builder["port"]

Bookmark with:
Technorati   Digg   Delicious   StumbleUpon   Facebook
My name is Jigar Desai I share my ideas on this site and you can contact me by filling contact form.

Categories