RMSP
RMSP

Reputation: 41

Using SMO to backup SQL Server 2005 Database with C# Windows Forms Application, Back up failed

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

Upvotes: 1

Views: 1513

Answers (2)

Bearington
Bearington

Reputation: 112

In order to backup a database, the process SQL Server is running as need to have the correct folder permissions on the folder the backup is targeted at.

From my experience the unless the SQL server process is running as the local admin it wont have permission to write to the root directory.

If you open SSMS and try to backup the database to your destination path you might encounter the same issue.

Try changing the backup folder to something other than the root of the drive or your my documents folder.

Upvotes: 1

Mushito
Mushito

Reputation: 1

Don' assign again the string value u just remove them and call the backup like this:

BackupSqlDatabase("YourDataBaseName","sa","password","ServerName","C:\\YourDataBaseName.bak");

Upvotes: 0

Related Questions