I just found about this little gem, I've seen all kinds of wicked code to create a sql connection string, anything from storing the whole connection string, to concatenating the string, to using a StringBuilder, etc. This class allows you to do all of the above but easier
SqlConnectionStringBuilder Class Note: This class is new in the .NET Framework version 2.0.Provides a simple way to create and manage the contents of connection strings used by the SqlConnection class
SqlConnectionStringBuilder Class (System.Data.SqlClient)
once you create an instance of that class, you can read/write to the individual items of the connection through properties or through the indexer as:
//you could pass the connection string in the constructor too
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
//using the indexer
builder["DataSource"] = "someDataSource";
//or using the property
builder.DataSource = "someDataSource";
this would be useful in a number of cases, for example if you had two connection strings that were different only by the server name
No comments:
Post a Comment