以下是批处理文件,同时也是还原数据的脚本文件MyRestore.bat
-------------------------------------------------------------------------
/*
@echo off
color 1e
clsecho.
echo ========= 复制数据库 并 解压缩 =========
md e:\AppDatabase
copy .\db.rar e:\AppDatabase\.
rar e -y E:\AppDatabase\db.rar e:\AppDatabase\echo.
echo ========= 还原数据库 =========
if not exist e:\AppDatabase\aTestapp.mdf goto restoreDB
echo.
color 4e
echo ********* 数据库已存在,即将退出 *********
pause.
goto end:restoreDB
echo.
echo === 开始还原 ===
pause.
echo 正在还原,请稍候...
wscript.exe //E:jscript %0%
*/// ========= 还原数据库 =========
oSvr = new ActiveXObject("SQLDMO.SQLServer");
oSvr.LoginSecure =true;
oSvr.Connect("(local)", "sa", "'1'");
oRes = new ActiveXObject("SQLDMO.Restore");
//oRes.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRes.Database = "aTestApp";
oRes.Files = "E:\\AppDatabase\\db";
oRes.FileNumber = 1;
oRes.ReplaceDatabase = false; //这句没效果,不管数据库存不存在,都会还原、覆盖。我只想让第一次安装时才还原,今后有数据后不能还原。
oRes.RelocateFiles = "[AppDatabase60_Data],[E:\\AppDatabase\\aTestApp.mdf],[AppDatabase60_Log],[E:\\AppDatabase\\aTestApp.ldf]";
oRes.SQLRestore(oSvr);
oSvr.Disconnect();/*
:end
*/
-------------------------------------------------------------------------

解决方案 »

  1.   

    ores.Action = SQLDMORestore_Database   '恢复类型数据库
    ores.Database = "Rcrs"                 '数据库名称
    ores.ReplaceDatabase = True            '替代现有数据库如不存则创建它
    ores.Files = dataFile                  '备份文件名,如果是多个设备上的需使用.Devices属性
    ores.DatabaseFiles = dataFile          '数据库文件此参数必须设置否则无法使用relocatefiles
    ores.FileNumber = 1                    '文件在设备上的ID号如果你只有一个文件就是1
      

  2.   

    A Boolean value that specifies whether a new image of the restored database will be created. If True, a new image of the database is created. The image is created regardless of the presence of an existing database with the same name. If False (default), a new image of the database is not created by the restore operation. The database targeted by the restore operation must exist on an instance of Microsoft SQL Server. 参考:
    http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.restore.replacedatabase(v=SQL.90).aspx
      

  3.   

    我就是想让这个 True 改为 False,不让人家替代已经存在的数据库。
    之所以做成 批处理文件 的形式,就是灵活,修改后不用编译即可直接用。用ini配置文件都没这么灵活。
    楼上的,这哪是VB的东东呀,是Sqlserver2000自带的功能,再说里边调用的脚本代码也是 Java语法。