Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating Connection String on-the-fly

0.00/5 (No votes)
26 Feb 2010 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here