views:

196

answers:

2

I have a winform app that will need to install SQLExpress with it. How can I predict what the SQL instance will be called so that my connection strings will all still work. ./SQLEXPRESS? username/SQLEXPRESS? or something else entirely?

Thanks!

A: 

Why do you have your connection strings hardcoded? You really should have the enduser have the option to change where the database is located.

Daniel A. White
Really, letting Nurses and Social Workers and other various "Common-Folk" choose there db is the community norm? I think our Help Desk will butcher me if I do that!Also, they would only be choosing from 1 db as there local is all they have available to them.My Connection string is in the Settings File currently.
Refracted Paladin
Then why are you worried if the db is not in the right name?
Daniel A. White
??? Don't I need to know the name of the local SQL instance for my connection string? Is there a default? Guaranteed?A La: "Data Source=WWCSTAGE;Initial Catalog=CMO;Persist Security Info=True;"What will Data Source Point to?
Refracted Paladin
+4  A: 

First, deploy the database as a data file. It will then be placed into the folder defined by ApplicationDeployment.DataDirectory (when it's deployed), or Application.StartupPath (when you're testing).

Then you need to check the context in which your program is running:

string databaseLocation;
if (ApplicationDeployment.IsNetworkDeployed)
{
    databaseLocation = ApplicationDeployment.CurrentDeployment.DataDirectory;
}
else
{  
    databaseLocation = System.Windows.Forms.Application.StartupPath;
}
databaseLocation = System.IO.Path.Combine(databaseLocation, "databasename.mdf");
RoadWarrior