成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

ASP.NET遠程備份恢復SQL Server數據庫

2010-08-28 10:52:10來源:西部e網作者:

///<summary>
      
///備份數據庫到本地磁盤
      
///</summary>
    
public bool BackUp(string BackUpFile)
    {
        try
        {
          
//第一步:在服務器上創建臨時文件夾
          ExecuteSql(@"master..xp_cmdshell 
'md C:\temp'");
          ExecuteSql(@"master..xp_cmdshell 
'del C:\temp\*.* /q'");
          
//第二步:備份數據庫到服務器目錄
          ExecuteSql(@"
backup database " + DataBaseName() + @" to disk='C:\temp\HSSY'");
          
//第三步:共享服務器的備份目錄
          ExecuteSql(@"master..xp_cmdshell 
'net share SQLDATABACK=C:\temp'");
          
//第四步:復制服務器上的備份文件到本地
          
File.Copy(@"\\" + ServerIP() + @"\SQLDATABACK\HSSY", BackUpFile,true);
          
          
return true;
        }
        catch (System.Data.SqlClient.SqlException E)
        {
          throw new Exception(E.Message);
        }
        finally
        {
          
//第五步:取消服務器共享目錄的共享
          ExecuteSql(@"master..xp_cmdshell 
'net share SQLDATABACK /delete'");
        }
    }

    
/// <summary>
    
/// 從本地磁盤恢復數據庫
    
/// </summary>
    
public bool Restore(string RestoreFile)
    {
        try
        {
          
//第零步:關閉用戶進程,防止其它用戶正在使用數據庫,導致數據恢復失敗
          KillServerUser();
          
//第一步:在服務器上創建臨時文件夾
          ExecuteSql(@"master..xp_cmdshell 
'md C:\temp'");
          ExecuteSql(@"master..xp_cmdshell 
'del C:\temp\*.* /q'");
          
//第二步:共享服務器的恢復目錄
          ExecuteSql(@"master..xp_cmdshell 
'net share SQLRESTORE=C:\temp'");
          
//第三步:復制服務器上的備份文件到本地
          
File.Copy(RestoreFile, @"\\" + ServerIP() + @"\SQLRESTORE\HSSY",true);
          
//第四步:取消服務器共享目錄的共享
          ExecuteSql(@"master..xp_cmdshell 
'net share SQLDATABACK /delete'");
          
//第五步:恢復數據庫到服務器目錄
          ExecuteSql(@"
restore database " + DataBaseName()+ @" from disk='C:\temp\HSSY'");
          
return true;
        }
        catch (System.Data.SqlClient.SqlException E)
        {
          throw new Exception(E.Message);
        }
        finally
        {
          
//第六步:取消服務器共享目錄的共享
          
//DbHelperSQL.ExecuteSql(@"master..xp_cmdshell 'net share SQLDATABACK /delete'");
        }
    }
關鍵詞:ASP.NETSQLServer

贊助商鏈接:

主站蜘蛛池模板: 仙桃市| 新营市| 若尔盖县| 额尔古纳市| 龙泉市| 靖宇县| 二连浩特市| 抚远县| 贵阳市| 东阿县| 合肥市| 怀仁县| 东宁县| 台山市| 四会市| 斗六市| 鄂州市| 通化市| 临泽县| 珲春市| 腾冲县| 彭阳县| 刚察县| 修武县| 浮梁县| 肥东县| 瓦房店市| 阿鲁科尔沁旗| 邵东县| 盐池县| 青川县| 曲沃县| 康平县| 西和县| 交口县| 贞丰县| 沂源县| 白朗县| 霍林郭勒市| 卓尼县| 白沙|