SQL Server connection string - how do you normally store or build

  • Thread starter Thread starter Man T
  • Start date Start date
M

Man T

I just wonder what is/are your practices to build the SQL Server connection
string?
Use object to build the connection string from config file or somewhere?
 
Man said:
I just wonder what is/are your practices to build the SQL Server connection
string?
Use object to build the connection string from config file or somewhere?

Usually you don't build the connection string dynamically. If you need
to connect to different databases you can keep sepearate connection
strings for them or create them from a template with a simple replacement.

The config file is a good place to store a connection string. Then you
don't have to recompile if the database settings change.
 
Man T said:
I just wonder what is/are your practices to build the SQL Server connection
string?
Use object to build the connection string from config file or somewhere?

If you are using version 2.0 or above of the Framework, you can use the
SqlConnectionStringBuilder class. You assign values to the various
properties of a SqlConnectionStringBuilder object, and you can then extract
the completed connection string from its ConnextionString property.
This is only needed if you are going to build it on-the-fly from values
that are "found out" during runtime. If the values in the connection string
are known beforehand, it is a common practice to simply store the completed
connection string in a config file.
 
Back
Top