neo.mn 0 Newbie Poster

Hi everyone,
I have developed a desktop application. I have created a menu item to click for backup database from MS SQL Server 2005. This is why I have created a method to perform the operation. But the method is not working well. There’s something wrong in the code which I cannot recognize. Please help me to find the problem and solution as well. The method code is given bellow:

public void BackupDatabase(String databaseName, String destinationPath)
        {
            try
            {                
                Backup sqlBackup = new Backup();
                sqlBackup.Action = BackupActionType.Database;
                sqlBackup.BackupSetDescription = "ArchiveDataBase:" + DateTime.Now.ToShortDateString();
                sqlBackup.BackupSetName = "Archive";
                sqlBackup.Database = databaseName;

                BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File);
                SqlConnection sqlCon = new SqlConnection(GenericDataAccess.GetConnectionString());
                ServerConnection connection = new ServerConnection(sqlCon);

                Server sqlServer = new Server(connection);
                Database db = sqlServer.Databases[databaseName];
                sqlServer.Databases.Add(db);                

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

                sqlBackup.Devices.Add(deviceItem);
                sqlBackup.Incremental = false;                

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

                sqlBackup.FormatMedia = false;
                sqlBackup.SqlBackup(sqlServer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n"  + ex.StackTrace);
            }
        }

And the click event from where I am calling the method is also given:

private void backupMnuItem_Click(object sender, EventArgs e)
{
            BackupData_Bl backup = new BackupData_Bl();
            backup.BackupDatabase("CFM", "D:\\TEMP");
}

For your information (it might help you to find the problem) I have first create my database using SQL Server Management Studio. Then I’ve move the database from sql server installation location to my project directory under a folder named DB. After that I have attached the database using SQL Server Management Studio.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.