Using SqlConnectionStringBuilder class in C#

Waqas Anwar
16 May 2008
30873 Views

Microsoft introduced a new class in ADO.NET 2.0 version which can be used to build database connection strings specific to the provider you are using in ADO.NET. Different database providers expose different properties for the connection strings. For example, SQL Server connection string properties are different than MySql or OleDb connection strings. Although, it is possible to concatenate strings to build a complete connection string but by using .NET connection string builder class you don't need to memorize the properties appropriate to specific provider.

In the following tutorial, I will show you how you can use SqlConnectionStringBuilder class available in System.Data.SqlClient namespace to build a simple connection string. 

SqlConnectionStringBuilder

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

builder.DataSource = TextBox1.Text;
builder.InitialCatalog = TextBox2.Text;
builder.UserID = TextBox3.Text;
builder.Password = TextBox4.Text;

TextBox5.Text = builder.ConnectionString;


Other than above mentioned four basic properties SqlConnectionStringBuilder also provides following common properties which can be used to build connection string with more options.

AsynchronousProcessing
AttachDBFilename
IntegratedSecurity
MaxPoolSize
MinPoolSize
MultipleActiveResultSets
PacketSize
Pooling
UserInstance
WorkstationID