text

Creating ConnectionString with SqlConnectionStringBuilder Class in C#

The SqlConnectionStringBuilder was introduced in .NET 2.0 that is used to build the database connection string specific for the SQL Server.

The MSDN defines it as “Provides a simple way to create and manage the contents of connection strings used by the SqlConnection class.”

How to Create ConnectionString in C#?

The SqlConnectionStringBuilder is defined in the namespace System.Data.SqlClient and can help you build the connection string or even parse an existing connection string to retrieve the properties.

private void Form1_Load(object sender, EventArgs e)
{
     SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder();
     connectionString.DataSource = @".\SQLEXPRESS";
     connectionString.InitialCatalog = "MyDatabase";
     connectionString.IntegratedSecurity = true;
     MessageBox.Show(connectionString.ConnectionString);
}
Creating ConnectionString with SqlConnectionStringBuilder Class in C#

Some other notable properties that SqlConnectionStringBuilder provides include

  • AsynchronousProcessing to indicate if the asynchronous processing is allowed for the connection .
  • UserID – Username for the SQL Server
  • Password – Password for the SQL Server
  • etc.

    3 Comments

  1. bi
    May 28, 2011
    Reply

    what a luck, exactly what I need for today 🙂 Lucky me 😀 Thx! Very useful snippet!

  2. May 28, 2011
    Reply

    🙂

  3. johny
    August 9, 2011
    Reply

    using System.Data.SqlClient;

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...