--是不是这个意思?
SQLString = "use master declare @Data_Path as varchar(100),@Log_Path as varchar(100) "
SQLString = SQLString + " select @Data_Path=ltrim(rtrim(filename)) from " & DatabaseName & "..sysfiles where charindex(''MDF'',filename)>0 "
SQLString = SQLString + " select @Log_Path=ltrim(rtrim(filename)) from " & DatabaseName & "..sysfiles where charindex(''LDF'',filename)>0 "
SQLString = SQLString + " RESTORE DATABASE " & DatabaseName & " FROM DISK = ''" & stLocation & "\" & DatabaseName & ".bak'' with replace, move ''" & DatabaseName & "_Data'' to @Data_Path, move ''" & DatabaseName & "_Log'' to @Log_Path "
SQLString = " exec('master..xp_cmdShell '' osql -U " & Me.Context.Parameters.Item("user") & " -P " & Me.Context.Parameters.Item("pwd") & " -S " & Me.Context.Parameters.Item("targetdir") & " -Q '' " & SQLString & " ') "
Dim sqlProcess As New System.Diagnostics.Process
sqlProcess.StartInfo.FileName = "osql.exe "
sqlProcess.StartInfo.Arguments = SQLString
ExecuteSql(connStr, "master", SQLString)
SQLString = "use master declare @Data_Path as varchar(100),@Log_Path as varchar(100) "
SQLString = SQLString + " select @Data_Path=ltrim(rtrim(filename)) from " & DatabaseName & "..sysfiles where charindex(''MDF'',filename)>0 "
SQLString = SQLString + " select @Log_Path=ltrim(rtrim(filename)) from " & DatabaseName & "..sysfiles where charindex(''LDF'',filename)>0 "
SQLString = SQLString + " RESTORE DATABASE " & DatabaseName & " FROM DISK = ''" & stLocation & "\" & DatabaseName & ".bak'' with replace, move ''" & DatabaseName & "_Data'' to @Data_Path, move ''" & DatabaseName & "_Log'' to @Log_Path "
SQLString = " exec('master..xp_cmdShell '' osql -U " & Me.Context.Parameters.Item("user") & " -P " & Me.Context.Parameters.Item("pwd") & " -S " & Me.Context.Parameters.Item("targetdir") & " -Q '' " & SQLString & " ') "
Dim sqlProcess As New System.Diagnostics.Process
sqlProcess.StartInfo.FileName = "osql.exe "
sqlProcess.StartInfo.Arguments = SQLString
ExecuteSql(connStr, "master", SQLString)
restore filelistonly from disk=c:\BACKUP\test.bak
这条语句执行的结果中直接取得LogicalName,
也就是在
move ''" & DatabaseName & "_Data'' to @Data_Path, move ''" & DatabaseName & "_Log'' to @Log_Path "中的" &DatabaseName & "_Data和" & DatabaseName & "_Log直接用LogicalName字段下的内容代替!!
create table #
(
LogicalName nvarchar(128),
PhysicalName nvarchar(260),
Type char(1),
FileGroupName nvarchar(128),
Size numeric(20,0),
MaxSize numeric(20,0)
)--插入文件信息
insert #
exec('restore filelistonly from disk=''c:\BACKUP\test.bak''')--查询
select * from #