Knowledge Transfer

Thursday, December 11, 2008

How to Build ConnectionString at Run-Time/Dynamically in ADO.Net

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();

This link is for Creating Class file fire DataAccess Leayer not For Above Stuff.
Data Access layer


Labels: , , , , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home