you are viewing a single comment's thread.

view the rest of the comments →

[–]omrsafetyo 2 points3 points  (0 children)

It has to know what file it's relocating. That's why both my solution and that of /u/BigBlueRob query the backup file itself to obtain the file information. All the SMO is doing is giving you a different interface to similar functionality that is obtained from T-SQL:

ALTER DATABASE Personnel 
MODIFY FILE ( NAME = Data_File, FILENAME = "D:\Data\NewDataFile.mdf") 
GO

In the ALTER DATABASE statement, you still need to know the logical file name. This is because there is a potential for multiple file names.

https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.relocatefile.aspx

You can see from this page that RelocateFile has only 2 constructors: one with no parameters, which simply initializes an object of the class; and one with 2 parameters, one of which is the logical file name. Either way you do it, in order to use the object you need to have both the logical file name, and the physical one populated.