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

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

贊助商鏈接:

主站蜘蛛池模板: 当涂县| 合江县| 长沙市| 华宁县| 吉木萨尔县| 酒泉市| 互助| 克拉玛依市| 屏山县| 曲阜市| 绍兴市| 凤翔县| 松滋市| 阜平县| 广水市| 安乡县| 平武县| 岳池县| 平南县| 新化县| 九龙坡区| 勐海县| 施秉县| 罗平县| 武山县| 镇原县| 茌平县| 东丰县| 新竹县| 十堰市| 桓仁| 阿巴嘎旗| 积石山| 沛县| 平顶山市| 拉萨市| 泸州市| 息烽县| 沂水县| 临沂市| 松江区|