Best way for a connection string init.

S

Stan Sainte-Rose

Hi Everybody,

What is the best way to declare the connection string like a variable.
I use sql server by my side and my friend will use msde.

I thought to use an udl file but it seems it works only for oledb
connection.

My string looks like this :
data source=<myServer>;initial catalog=<myCatalog>;integrated
security=SSPI;persist security info=False;workstation id=<myComputer>;packet
size=4096

and of course, for my friend, myServer, myCatalog have not the same names.
Does it mean, I have to compile a version with my own parameters (for my
tests) and one for my friend's parameters
or is there a hint ?

Stan
 
M

Miha Markic

Hi Stan,

Include your connection string in config file of your app.
For connection strings details see www.connectionstring.com.

Place the following line (under <configuration> element) in app.config file
of your app.
<appSettings file="user.config">
Add a user.config file to your project.

It should like something like:
<appSettings>
<add key="ConnString" value="yourconnectionstring" />
</appSettings>

Make sure that user.config files is copied to executing folder by:
Go to Project properties, select Build Events, add the following line:
if exist "$(ProjectDir)user.config" copy "$(ProjectDir)user.config"
"$(TargetDir)"

Read the connection string in your app:
string connString =
System.Configurations.ConfigurationSettings.AppSettings["ConnString"];

There you go.
 
W

William Ryan

Storing it in a .config file (encrypted if possible) is a great way to do
this. Then you edit it or have your install script write to it.

Then you can reference it something like this...

Dim cn as SqlConnection
[cn = New
SqlConnection(ConfigurationSettings.AppSettings.Item("connectString"].ToStri
ng());

You may want to run a decrypt routine, that way it can't be read or used
without your app.

HTH,

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top