在ASP.NET 中备份和恢复SQLServer 2005.

因为在一个ASP.NET项目中需要集成SQLSERVER 2005的备份和恢复功能.正如网上大多数文章所说.备份关不复杂.存储过程即可解决问题.恢复颇为费神.一开始就想到了使用SQL SERVER 2005和托管编程.
编译调试报错:
    The specified module could not be found. (Exception from HRESULT: 0x8007007E) 
    百思不得其解,后升级SQL SERVER 2005 至 SP2.问题解决:-)
    但是具体是什么BUG因为时间原因未深究.这里贴出来让遇到同样问题有朋友指个路,少走弯路.具体恢复代码:
   

/*=====================================================================================
    *Name:Restore DataBase
    *Author:Boon Chu
    *Date: 2007-12-10
    *KeyWord:Microsoft.SqlServer.Management.Smo
    =====================================================================================
*/

     
public override bool DBRstore(string filename)
     
{            
            
try{
                
string sername = conn.Split(";".ToCharArray())[0].Replace("Data Source=","");
                Server _serv 
= new Server(sername);
                Database _swdb 
= _serv.Databases["MYDATABSE"];
                
if (_swdb.DatabaseOptions.UserAccess != DatabaseUserAccess.Single)
                
{
                    _swdb.DatabaseOptions.UserAccess 
= DatabaseUserAccess.Single;
                    _swdb.Alter();
                }

                BackupDeviceItem bdi 
= new BackupDeviceItem(filename, DeviceType.File);
                Restore _r 
= new Restore();
                _r.Action 
= RestoreActionType.Database;
                _r.NoRecovery 
= false;
                _r.ReplaceDatabase 
= true;
                _r.Devices.Add(bdi);
                _r.Database 
= "SWO2_07";
                _r.SqlRestore(_serv);
            }

            
catch (Microsoft.SqlServer.Management.Common.SqlServerManagementException e)
            
{
                
throw new Exception(e.Message);
            }

            
return true;
    }


 

posted @ 2007-12-10 08:49  CalvinChu  阅读(533)  评论(2编辑  收藏  举报