Click here to Skip to main content
15,883,837 members
Articles / Web Development / ASP.NET
Tip/Trick

Creating Connection String on-the-fly

Rate me:
Please Sign up or sign in to vote.
1.89/5 (3 votes)
26 Feb 2010CPOL 9.8K   2  
It is possible for us to create and manage Connection String at run time by using SqlConnectionStringBuilder class, So we can change connectionstring at any time. Example Code : SqlConnectionStringBuilder ConnBuild = new SqlConnectionStringBuilder();ConnBuild.DataSource =...
It is possible for us to create and manage Connection String at run time by using SqlConnectionStringBuilder class, So we can change connectionstring at any time.

Example Code :

SqlConnectionStringBuilder ConnBuild = new SqlConnectionStringBuilder();

ConnBuild.DataSource = "SQLExpress";
ConnBuild.ConnectTimeout = 1000;

ConnBuild.IntegratedSecurity = true;
//or
ConnBuild.IntegratedSecurity = false;
ConnBuild.UserID = "Indrajeeth";
ConnBuild.Password = "Indrajeeth";

Response.Write(cnbuilder.ConnectionString); 


Some important properties of connectionstringbuilder class
InitialCatalog -
Used for returning or setting name of the database related with the connection.

IntegratedSecurity -
sets or returns false - indicating user-id and password is specified. and sets or returns false when current windows account credentials are used for authentication

UserID -
Used for returning or setting user ID while connecting to SQL Server.

Password -
Gets or sets the password for the SQL Server account.

ConnectTimeout -
Used for setting or returing the time span (in seconds) for waiting connection to the server before terminating.

DataSource -
Used for setting or returing the name or network address for connecting of the instance of SQL Server.

Encrypt -
Returns or sets Boolean value for specifing whether SQL Server uses SSL encryption for all data sent between the client and server (applicable only when the server has a certificate installed.)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior) Excel Informatics, Pune
India India
Am Indrajeet T. Sutar. I am a web developer. I am working on .net technology. I have completed my Masters in Computers and Management (people call it as MCA, MBA etc.) Apart from programming i do photography (not regularly though), traveling and reading books.

Comments and Discussions

 
-- There are no messages in this forum --