1

I am trying to create a backup of my SQL Server Database in Visual Studio 2010.

I am using the following command to back up the database:

BACKUP DATABASE Db TO DISK = 'c:\server.bak'

This is connection string that I am using.

<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Db.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

However, when I run the command, I get the following error message.

Database 'Db' does not exist.

Here is a screenshot that describes my error:

http://www.cs.purdue.edu/homes/aerfanfa/sqlerror.jpg

Does anyone have a solution for my problem? Thanks!

2 Answers 2

1

The problem is: your database db.mdf is not attached to the SQL Server Express instance - therefore, you cannot back it up.

This whole AttachDbFileName=..... story is a bit tricky - and quite a mess, if you ask me.

I would recommend to just forget about using that AttachDbFileName=... stuff, and instead:

  • just attach the db.mdf file to your local SQL Server Express instance (using SQL Server Management Studio Express)

  • talk to the attached database using it's logical database name instead of messing around with a .mdf file

Once you've done that - then you can use commands like BACKUP DATABASE ... and everything else!

Your connection string would be much simpler, too!

<add name="ConnectionString" 
     connectionString="Server=.\SQLEXPRESS;Database=YourDatabaseName;Integrated Security=True;"
     providerName="System.Data.SqlClient"/>
Sign up to request clarification or add additional context in comments.

Comments

0

Try right clicking the database in Manament studio, selecting tasks, then "Backup". After setting the parameters, you can select "Script" which will give you a proper backup script.

A backup script looks something like:

BACKUP DATABASE [Db] TO  DISK = 
N'C:\Server.bak'
 WITH NOFORMAT, INIT,  NAME = N'Db Backup', SKIP, NOREWIND, NOUNLOAD, 
STATS = 10
GO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.