Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I can't take backup of sql database using following code.please help me out as soon as possible.
public void BackupDatabase(String databaseName, String userName,
           String password, String serverName, String destinationPath)
               string drop_down_value = cmbDatabase.SelectedItem.ToString();

               string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename='" + drop_down_value + "';Integrated Security=True;User Instance=True";

               Backup sqlBackup = new Backup();

               sqlBackup.Action = BackupActionType.Database;
               sqlBackup.BackupSetDescription = "ArchiveDataBase:" +
               sqlBackup.BackupSetName = "Archive";

               sqlBackup.Database = "event.mdf";

               BackupDeviceItem deviceItem = new BackupDeviceItem("D:\\test.bak", DeviceType.File);
               SqlConnection sqlCon = new SqlConnection(con);
               ServerConnection connection = new ServerConnection(sqlCon);
               //  ServerConnection connection = new ServerConnection(serverName, userName, password);
               Server sqlServer = new Server(connection);

               Database db = sqlServer.Databases[drop_down_value];

               sqlBackup.Initialize = true;
               sqlBackup.Checksum = true;
               sqlBackup.ContinueAfterError = true;

               sqlBackup.Incremental = false;

               sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
               sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

               sqlBackup.FormatMedia = false;

               MessageBox.Show("The support of the database was successfully performed", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);
           catch (Exception ex)
Updated 9-May-13 9:14am
ZurdoDev 9-May-13 15:25pm    
Why? Is there an error?
Dev Gupta from Mumbai,India 9-May-13 15:30pm    
there's no error.But a backupfile is not created at given location.
Jason Gleim 9-May-13 15:28pm    
You haven't told us what the error is or what you have tried using the above code.please help us out as soon as possible.

And DON'T post the additional information as a solution. Edit your question please!
Dev Gupta from Mumbai,India 9-May-13 15:30pm    
there's no error.But a backupfile is not created at given location.
ZurdoDev 9-May-13 15:34pm    
What is Backup? Is that a custom class?

You may easily carry out backup and if it need restore sql database with the help of [spam link removed]

This article will help you to make it
Share this answer
There are a huge number of possible reasons why a backup may fail.
Try this: Backing up an SQL Database in C#[^]
Share this answer
Dev Gupta from Mumbai,India 9-May-13 15:45pm    
I'm getting error as "backup failed for server" at line

ServerConnection con = new ServerConnection(@"xxxxx\SQLEXPRESS");
What to do now?
OriginalGriff 9-May-13 15:55pm    
Look at the error and see if there is any inner error.
Did you follow all the steps in the tip.
Dev Gupta from Mumbai,India 9-May-13 15:56pm    
Dev Gupta from Mumbai,India 11-May-13 15:44pm    
I am getting error as "could not locate entry in sys database for database name".How to fix this error?
OriginalGriff 12-May-13 3:27am    
That implies that it is not an SQL database your are trying to back up. It wouldn't be an MDF file that you temporarily attach, would it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900