views:

61

answers:

1
+1  Q: 

Backup Sql Express

I would like to be able to run an on demand backup of a .Net MVC app's SQL Express 2008 database to eg a flash stick plugged into the machine running the app.

I tried

QuickstemDataContext db = new QuickstemDataContext();
 string quickstem_path = Path.Combine(save_path, "quickstem.backup");
 db.ExecuteCommand(string.Format("BACKUP DATABASE {1} TO DISK = '{0}' WITH COMPRESSION;", quickstem_path, db.Mapping.DatabaseName));

But get the exception

Database 'quickstem' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally

I am using the following connection string.

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\quickstem.mdf;Integrated Security=True;User Instance=True"

Do I need to attach the DB using something like Express Management Studio and give it a name etc. Ideally I want to keep the app deploy very simple without having to setup sql management studio etc. Can this attaching be done another way or can a Backup be done with out needing to attach

I tried giving it the full path of the .mdf file instead of the database name but got a syntax error on c:

+1  A: 

Hi Matt

You'll find that if you add Database=Quickstem to your connection string, your backup code will work just fine.

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\quickstem.mdf;Integrated Security=True;User Instance=True;Database=Quickstem
Jimbo