2011年8月23日 星期二

SQLDMO 備份和還原Microsoft SQL Server 資料庫

///

/// 数据库备份

/// 参数信息要备份的数据库的名称Backup .Database = "Northwind";

/// 另外一个是存放备份文件的路径和文件名称Backup .Files = @"c:\databak.bak";

///


private void Button1_Click(object sender, System.EventArgs e)

{

SQLDMO.Backup Backup = new SQLDMO.BackupClass();

SQLDMO.SQLServer SQLServer = new SQLDMO.SQLServerClass();

try

{

SQLServer.LoginSecure = false;

SQLServer.Connect("IP", "USERNAME", "PWD");

Backup .Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMBackup _Database;

Backup .Database = "Northwind";//要备份的数据库的名称,例如Northwind

Backup .Files = @"c:\databak.bak";//存放路径,和备份文件的名称

Backup .BackupSetName = "Northwind";

Backup .BackupSetDescription = "数据库备份";

Backup .Initialize = true;

Backup .SQLBackup(SQLServer);

}

Catch(Exception ex)

{

Throw ex;

}

finally

{

SQLServer.DisConnect();

}

}

///

/// 数据库恢复

/// 参数信息要恢复的数据库的名称Restore .Database = "Northwind_Res";

/// 另外一个是存放数据库备份文件的路径和文件名称

/// Backup .Files = @"c:\databak.bak";用来恢复数据库

///


private void Button2_Click(object sender, System.EventArgs e)

{

SQLDMO.Restore Restore = new SQLDMO.RestoreClass();

SQLDMO.SQLServer SQLServer = new SQLDMO.SQLServerClass();

try

{

SQLServer.LoginSecure = false;

SQLServer.Connect("IP", "USERNAME", "PWD");

Restore .Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMRestore _Database;

Restore .Database = "Northwind_Res";

Restore .Files = @"C:\ databak.bak";

Restore .FileNumber = 1;

Restore .ReplaceDatabase = true;

Restore .SQLRestore(SQLServer);

}

catch(Exception ex)

{

Throw ex;

}

finally

{

SQLServer.DisConnect();

}

}

}

沒有留言:

張貼留言