AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 How to Build ConnectionString at Run-Time/Dynamically
Posted by Moderator1 on  6/16/2008 7:22:55 AM 2849 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

Featured Resources:
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time.
Simply SQL - Free 111 Page Preview!
Simply SQL is a practical step-by-step guide to writing SQL.
100% Free Subscription until Stock lost.View complete list.
Sponsored by