Wednesday, May 04, 2005

Peek .NET 2005: Untangling Connection strings

Did you ever thought about instances when the conection string becomes messy and why it is just not OO. And did you ever come across problems of extracting certain information out of your connection string?Well .NET 2005 has a cool way of doing this with the introduction of connection string builders. It allows you to create the connection string by setting properties to the connection string builder objects and then extracting the connection string from it. You can also pass in a connection string and extract information out of it easily.

For example:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.ConnectionString = "workstation id=PRASANNA;packet size=4096;integrated security=SSPI;data source=PRASANNA;persist security info=True;initial catalog=SampleDB";
Console.WriteLine(builder.InitialCatalog);

This will display the initial catalog("SampleDB") extracted from the connection string using the SQL Server specific connection builder object. Now that looks much cleaner that writing code by splitting the connection string and so on. There are different connection builders available for different supported RDBMS as well as a generic connection string builder.

No comments: