Mahmood Jenami
Mahmood Jenami

Reputation: 431

How to backup and restore localDb 2012 Database?

I try to backup my localDB file using file.Copy() method but it throws the following IOException:

The process cannot access the file '.\DB.mdf' because it is being used by another process.

And when I try to do backup with the following script:

@"BACKUP DATABASE " + DatabaseName + " TO DISK = N'" + BackUpLocation + @"\" + BackUpFileName + @"'"  

it throws the following SqlException:

Database 'DB' does not exist. Make sure that the name is entered correctly.

My connection string is:

Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\DB.mdf;Integrated Security=True;Connect Timeout=30

What is the best way to do backup in this situation?

Upvotes: 3

Views: 5305

Answers (1)

Vojtěch Dohnal
Vojtěch Dohnal

Reputation: 8104

Use the full path od your mdf (e.g. C:\DATA\DB.MDF) as the database name in your BACKUP DATABASE command.

Upvotes: 2

Related Questions