AspdotnetCodes.com
Voted 'Best ASP.NET Host' for 2008 – DiscountASP.NET
 
Articles Subscribe for our Articles Updates
Books
Resources
Downloads
Free Tech Magazines
Archives
Softwares
Newsletter
Suggest Us
Link to Us
 How to Build ConnectionString at Run-Time/Dynamically
Posted by Moderator1 on  6/16/2008 7:22:55 AM 335 Views
 
SqlConnectionStringBuilder is a class that helps to create and manage connection strings at run-time. You can change any connection string setting through your code whenever and wherever you want.

Sample Code for SqlConnectionStringBuilder:

SqlConnectionStringBuilder cnbuilder = new SqlConnectionStringBuilder();

cnbuilder.DataSource = "MyServerName";
cnbuilder.ConnectTimeout = 1000;
cnbuilder.IntegratedSecurity = false;
cnbuilder.UserID = "sa";
cnbuilder.Password = "MyDBServerPassword";

Response.Write(cnbuilder.ConnectionString);


The above block of code, create a connectionstring based on the values you passed to the DataSource, UserId and Password fields. You can directly use this SqlConnectionStringBuilder object to open a connection as follows


SqlConnection cn = new SqlConnection(cnbuilder.ConnectionString);
cn.Open();

Response.Write("Connected.....");

Post New Tips/Tricks View all Tips/Tricks

Sponsored by