MM的SQLDMO- -哈哈(数据备份与恢复篇)

本文介绍了一个使用SQLDMO实现数据库备份和恢复的示例代码。通过构造DbOper类的方法,可以方便地进行数据库的备份和恢复操作。文章提供了具体的代码实现,并详细说明了如何设置备份路径、备份说明等参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引用



None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
None.gif
public sealed class DbOper 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// DbOper类的构造函数
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private DbOper() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 数据库备份
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  public static void DbBackup() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   SQLDMO.Backup oBackup 
= new SQLDMO.BackupClass(); 
InBlock.gif   SQLDMO.SQLServer oSQLServer 
= new SQLDMO.SQLServerClass(); 
InBlock.gif   
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    oSQLServer.LoginSecure 
= false
InBlock.gif    oSQLServer.Connect(
"localhost""usa""pwd"); 
InBlock.gif    oBackup.Action 
= SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
InBlock.gif   
//--数据库名称
InBlock.gif
    oBackup.Database = "MM";
InBlock.gif    
//--备份路径
InBlock.gif
    oBackup.Files = @"d:\MM.bak";
InBlock.gif    
//--备份说明
InBlock.gif
    oBackup.BackupSetName = "MM"
InBlock.gif    oBackup.BackupSetDescription 
= "数据库备份"
InBlock.gif    oBackup.Initialize 
= true
InBlock.gif    oBackup.SQLBackup(oSQLServer); 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
throw
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    oSQLServer.DisConnect(); 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 数据库恢复
InBlock.gif  
/// </summary> 
ExpandedSubBlockEnd.gif  
/// 恢复数据库的时候必须切断连接

InBlock.gif  public static void DbRestore() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   SQLDMO.Restore oRestore 
= new SQLDMO.RestoreClass(); 
InBlock.gif   SQLDMO.SQLServer oSQLServer 
= new SQLDMO.SQLServerClass(); 
InBlock.gif   
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    oSQLServer.LoginSecure 
= false;
InBlock.gif    oSQLServer.Connect(
"localhost""usa""pwd"); 
InBlock.gif    oRestore.Action 
= SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
InBlock.gif    oRestore.Database 
= "MM";
InBlock.gif    oRestore.Files 
= @"d:\MM.bak";
InBlock.gif    oRestore.FileNumber 
= 1
InBlock.gif    oRestore.ReplaceDatabase 
= true
InBlock.gif    oRestore.SQLRestore(oSQLServer); 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
throw
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    oSQLServer.DisConnect(); 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
ExpandedBlockEnd.gif}
 

转载于:https://www.cnblogs.com/ajaxren/archive/2007/04/28/730497.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值