Using SqlConnectionStringBuilder class in C#

You are currently viewing Using SqlConnectionStringBuilder class in C#

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 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

READ ALSO:  Navigate Master/Details Tables in DataSet using DataRelation

This Post Has 4 Comments

  1. Gentleman

    Sir I request you, please add more in this tutorial about storing this connection string into app.config or web.config.

  2. Nice Guy

    I want to know, How can I check that “file or file path” is correct before opening the connection. So that exception may not accur.

    1. Waqas Anwar

      string filePath = “C:\abc.txt”;
      FileInto file = new FileInfo(filePath);

      // Process your File here.

  3. Huma

    i want to know that how to use Datalist control . Actually i want to show the category list through db .

Leave a Reply