This is my code to back up my database I followed this tutorial, as you can see, the codes are identical. The error comes after I fill in the variables. The error I get is
Backup failed for Server 'RITZEL-PC\SQLEXPRESS'.
Back up function:
public void BackupDatabase(String databaseName, String userName, String password, String serverName, String destinationPath)
{
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);
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
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);
}
Button click
private void BackUp_Btn_Click(object sender, EventArgs e)
{
String databaseName = @"D:\MY_THESIS\WORKING FILES\NNIT-RMS.mdf";
String userName = "NNIT-Admin";
String password = "password";
String serverName = @"RITZEL-PC\SQLEXPRESS";
String destinationPath = @"D:\";
BackupDatabase(databaseName,userName,password,serverName,destinationPath);
}
Database Information I am not allowed to post images yet but I have uploaded a screen shot. See here: http://img268.imageshack.us/img268/9250/sqlg.jpg
databaseNameshould be the logical database name of the database when it's attached to the SQL Server Express - e.g.MYDATABASE- and not the entire file name of the.mdffile. I don't think SMO allows you to back up a separate.mdffile.mdffile with SMO. You need to attach the.mdffile to your SQL Server Express, give it a logical database name, and then use that logical database name for your SMO backup. This whole "attach .mdf on the fly" feature is a bit of a mess and quite frankly should not be used - and will be discontinued in SQL Server 2012 ("Denali")