简体   繁体   中英

backing up database in visual studio c#

I am developing a windows application using c# and inbuilt SQL Server on Visual Studio 2010.

I am trying to create a click event to backup database to a specified location, but when I am running it, it is showing me error that database with this name doesn't exist.

the code I am using is

 SqlCommand cmd = new SqlCommand("BACKUP DATABASE GRTU_LIBRARY_MANAGEMENT TO  DISK = N'D:\temp\foo' WITH NOFORMAT, NOINIT,  NAME = N'Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10", connect);
 cmd.ExecuteNonQuery();

It is showing error "Database 'GRTU_LIBRARY_MANAGEMENT' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally."

I am using the same database in the whole application but here it is showing this error. I am using the inbuilt SQL server in Visual Studio 2010

Please help

Seems like you missed the keyword DATABASE . Add DATABASE after BACKUP and check

SqlCommand cmd = new SqlCommand("BACKUP DATABASE GRTU_LIBRARY_MANAGEMENT TO  DISK = N'D:\temp\foo' WITH NOFORMAT, NOINIT,  NAME = N'Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10", connect);

Sounds to me like it's looking at the wrong server. Perhaps you could swap in "SELECT @@SERVERNAME" for the backup statement and look at the results, just to verify it's running against the server you expect.

It cannot find the file you are backup to. please change the text of 'D:\\temp\\foo' to 'D:\\Temp\\foo\\Test.bak'. Please remember that the directory D:\\Temp\\foo must exist. The whole text looks like this: (change the 'Test' to your own database name) SqlCommand cmd = new SqlCommand(@"BACKUP DATABASE Test TO DISK = N'D:\\Temp\\foo\\Test.bak' WITH NOFORMAT, NOINIT, NAME = N'Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10", connect);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM