SQLConnectionBuilder Class
So there I was perusing the Beta MSDN Documentation when I came across this little ditty.
MS has finally decided to give us a class that lets us deal with a connection string in terms of properties as opposed to a straight out string. So now you can do stuff like “builder.Password = someNewPassword”.
Wadda ya know !
using System;
using System.Data.SqlClient;
using System.Collections;
class Program
{
static void Main()
{
// Create a new SqlConnectionStringBuilder and
// initialize it with a few name/value pairs.
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder(GetConnectionString());
// The input connection string used the
// Server key, but the new connection string uses
// the well-known Data Source key instead.
Console.WriteLine(builder.ConnectionString);
// Pass the SqlConnectionStringBuilder an existing
// connection string, and you can retrieve and
// modify any of the elements.
builder.ConnectionString = “server=localhost;user id=MyUserName;” +
“password=MyPassword;initial catalog=AdventureWorks”;
// Now that the connection string has been parsed,
// you can work with individual items.
Console.WriteLine(builder.Password);
builder.Password = “NewPassword”;
builder.AsynchronousProcessing = true;
// You can refer to connection keys using strings,
// as well. When you use this technique (the default
// Item property in Visual Basic, or the indexer in C#),
// you can specify any synonym for the connection string key
// name.
builder[”Server”] = “.”;
builder[”Connect Timeout”] = 1000;
builder[”Trusted_Connection”] = true;
Console.WriteLine(builder.ConnectionString);
Console.WriteLine(”Press Enter to finish.”);
Console.ReadLine();
}
private static string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file using the
// System.Configuration.ConfigurationSettings.AppSettings property.
return “Server=localhost;Integrated Security=SSPI;” +
“Initial Catalog=AdventureWorks”;
}
}
Write a comment