Technisches‎ > ‎

How to backup a sql database via C#

References:
Microsoft.SqlServer.Rmo
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoEnum
Microsoft.SqlServer.SqlEnum
Microsoft.SqlServer.ConnectionInfo
...

using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Common;

using System.Data.SqlClient;

...

public static void BackupDatabase(SqlConnection _connection, string _nameOfDatabase, string _backupDeviceDefaultName, string _targetpath)

{

    Server SmoServer = new Server(new ServerConnection(_connection));

 

    if (string.IsNullOrEmpty(_targetpath))

        throw new Exception("Es gibt keinen Sicherungspfad! - Datenbank kann nicht gesichert werden");

 

    try

    {

        Backup backup = new Backup();

        backup.Action = BackupActionType.Database;

        backup.Database = _nameOfDatabase;

        backup.Devices.Add(new BackupDeviceItem(_targetpath, DeviceType.File));

        backup.SqlBackup(SmoServer);

    }

    catch (Exception ex)

    {

        throw ex;

    }

}


Comments