简体   繁体   English

以编程方式还原Microsoft SQL Server数据库

[英]Programmatically Restore a Microsoft SQL Server Database

I wish to use ColdFusion to grab a database backup from a live server and restore it into a test environment. 我希望使用ColdFusion从实时服务器中获取数据库备份并将其还原到测试环境中。 The grabbing bit is done, but I can't find a way of restoring the database programatically forcibly overwriting the database if its already there. 抓取操作已经完成,但是我找不到一种以编程方式强制恢复数据库的方法,如果数据库已经存在,则将其强制覆盖。

Any help would be appreicated, I figured there should be some SQL script or batch file that could do the job for me. 任何帮助将不胜感激,我认为应该有一些SQL脚本或批处理文件可以为我完成这项工作。

Obviously windows environment. 显然是Windows环境。 SQL 2008, ColdFusion 9. SQL 2008年ColdFusion 9。

----Put database into single user mode (terminates open connections - else restore fails)
ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE


RESTORE DATABASE YourDB 
FROM DISK = 'D:\temp\YourDB.bak' 
WITH REPLACE 
,MOVE 'YourDB_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\YourDB_Data.mdf'
,MOVE 'YourDB_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\YourDB_Data.ldf'


ALTER DATABASE YourDB SET MULTI_USER
GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM