如何使数据库备份文件仅包含不同的数据

时间:2014-08-15 02:59:00

标签: c# sql-server sql-server-2008-r2

在备份我的SQL Server 2008 R2数据库的Windows应用程序中,备份文件将包含数据库架构和表数据,但是当尝试进行备份时,备份文件的大小会越来越大

有没有办法只在最近的文件中保留备份?

这是用于备份的代码

connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);

string location = "";
SqlDataReader reader = null;

try
{
    reader = DataAccess.SelectCommand(StoredName,null);

    while (reader.Read())
    {
        location = reader[0].ToString();
    }

    reader.Close();
}
catch (Exception ex)
{
    reader.Close();
    connection.Close();

    MessageBox.Show(string.Format("Exception occured.\nMessage: {0}", ex.Message), "fawzy Mokhtar", MessageBoxButtons.OK, MessageBoxIcon.Error);

    return 0;
}

Server sqlServer = new Server(new ServerConnection(connection));

Backup dbBackup = new Backup();

try
{
    dbBackup.Action = BackupActionType.Database;
    dbBackup.Database = DBName;
    dbBackup.BackupSetName = string.Format("{0} backup set.", DBName);
    dbBackup.BackupSetDescription = string.Format("Database: {0}. Date: {1}.", DBName, DateTime.Now.ToString("dd.MM.yyyy hh:m"));
    dbBackup.MediaDescription = "Disk";

    BackupDeviceItem device = new BackupDeviceItem(location + DBName + ".bak", DeviceType.File);
    dbBackup.Devices.Add(device);

    dbBackup.SqlBackup(sqlServer);
    return 1;
}
catch (Exception exc)
{
    dbBackup.Abort();

    MessageBox.Show(string.Format("Exception occured.\nMessage: {0}", exc.Message), "fawzy mokhtar", MessageBoxButtons.OK, MessageBoxIcon.Error);

    return 0;
}
finally
{
    connection.Close();
}

1 个答案:

答案 0 :(得分:0)

正如marc_s在评论中所说,你正在寻找差异备份。要使用您所拥有的内容执行此操作,请将备份对象的Incremental属性设置为true。请确保您不时仍在进行完整备份!