BAT代码:
@echo off Title DataBase Color 0A :caozuo echo. echo ═══════════════════════════════════════ echo 【数据库操作】: echo 1.删除数据库 echo 2.还原数据库 echo 3.操作3 echo 4.操作4 echo 5.操作5 set /p n=输入操作号: if "%n%"=="" cls&goto :caozuo if "%n%"=="1" call :1 if "%n%"=="2" call :2 if "%n%"=="3" call :3 if "%n%"=="4" call :4 if "%n%"=="5" call :5 if /i "%n%"=="n" exit pause goto :eof :1 echo. 删除数据库开始………… @osql.exe /Usa /[email protected] /Slocalhost /dmaster /i C:\Users\310171611\Desktop\142\database\DropDB.sql echo. 删除数据库结束………… goto :caozuo :2 echo. 还原数据库开始………… @osql.exe /Usa /[email protected] /Slocalhost /dmaster /i C:\Users\310171611\Desktop\142\database\RestoreDB.sql echo. 还原数据库结束………… goto :caozuo goto :caozuo :3 echo 操作3 goto :caozuo :4 echo 操作4 goto :caozuo :5 echo 操作5 goto :caozuo
bat文件中调用的是 dos命令 osql,大家对应的修改相应的链接字符串即可
RestoreDB.sql,DropDB.sql 为常用的sql文件,可以换成任意的sql
DropDB.sql
use master go alter database dbEMS set single_user with rollback immediate --将数据库回滚到原始配置状态 go drop database dbEMS--删除数据库 GO use master go alter database dbEMSConfig set single_user with rollback immediate --将数据库回滚到原始配置状态 go drop database dbEMSConfig--删除数据库 GO
RestoreDB.sql
USE MASTER go if db_id(‘dbEMS‘) is not null DROP DATABASE dbEMS GO create DATABASE dbEMS RESTORE DATABASE dbEMS FROM DISK = ‘C:\Users\310171611\Desktop\142\database\dbEMS_A.bak‘ WITH MOVE ‘dbEMS‘ TO ‘C:\database\dbEMS.mdf‘, MOVE ‘dbEMS_log‘ TO ‘C:\database\dbEMS_log.ldf‘, STATS = 10, REPLACE GO declare @msg varchar(70) if (@@ERROR <> 0 ) begin select @msg=convert(char(26),getdate(),9)+‘-----dbEMS还原数据失败或出现异常‘ print @msg end else begin select @msg=convert(char(26),getdate(),9)+‘-----dbEMS数据库还原完毕‘ print @msg END ------------------------------------------------------------------------- USE MASTER go if db_id(‘dbEMSConfig‘) is not null DROP DATABASE dbEMSConfig GO create DATABASE dbEMSConfig RESTORE DATABASE dbEMSConfig FROM DISK = ‘C:\Users\310171611\Desktop\142\database\dbEMSConfig_A.bak‘ WITH MOVE ‘dbEMSConfig‘ TO ‘C:\database\dbEMSConfig.mdf‘, MOVE ‘dbEMSConfig_log‘ TO ‘C:\database\dbEMSConfig_log.ldf‘, STATS = 10, REPLACE GO declare @msg varchar(70) if (@@ERROR <> 0 ) begin select @msg=convert(char(26),getdate(),9)+‘-----dbEMSConfig还原数据失败或出现异常‘ print @msg end else begin select @msg=convert(char(26),getdate(),9)+‘-----dbEMSConfig数据库还原完毕‘ print @msg end
其他的附加,分离的功能大家可以根据自己的情况来写sql脚本。
时间: 2024-10-19 01:12:28