【问题标题】:restore sql database恢复sql数据库
【发布时间】:2010-09-27 05:54:20
【问题描述】:

我想在 C# 中使用 asp.net Web 应用程序恢复数据库.. 谁能帮我解决这个问题?

【问题讨论】:

    标签: c# asp.net sql-server restore


    【解决方案1】:

    您必须使用以下命名空间

    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Common;
    

    简单例子

    public bool RestoreDB(string dbName, string backupPath, string newLocation,string userName,sring Password)
            {            
                ServerConnection con = new ServerConnection(serverName);
                if(userName=="")
                    con.LoginSecure = true;
                else{
                    con.LoginSecure = false;
                    con.Login = userName;
                    con.Password = Password;         
                }
                Restore restoreObj = new Restore();         
                var srv = new Server(con);
                if (srv != null)
                {
                    restoreObj.Action = RestoreActionType.Database;
                    restoreObj.Database = dbName;
                    BackupDeviceItem resDevice = new BackupDeviceItem(backupPath, DeviceType.File);
                    restoreObj.PercentCompleteNotification = 10;
                    restoreObj.ReplaceDatabase = false;                
                    restoreObj.Devices.Add(resDevice);                 
    
                    restoreObj.PercentComplete += (sender, evtargs) =>
                    {
                       //on progress callback
                    };
    
                    restoreObj.Complete += (sender, evtargs) =>
                    {
                       //on competion callback
                    };
    
                    restoreObj.SqlRestoreAsync(srv);
    
                }
                return true;
            }
    

    【讨论】:

      猜你喜欢
      • 2016-04-04
      • 1970-01-01
      • 2021-12-23
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多