Group: General Forum Members
Last Login: Friday, November 29, 2013 6:34 AM
I am having issues changing the logical names post restore.
I am restoring a database with SMO (using VB.NET). After the database has been restored, I am changing the logical names of the files to match the new database name by using an alter database statement query.
Before the restore is performed.I am doing a validation for valid database names (i.e. valid windows OS filenames) via a regex pattern before performing the restore. The problem is, the T-SQL that I am executing after the restore is not adequately compensating for special characters in the database name (i.e. filename). Here are the statements
Taken from SMO SQL capture during a VB.NET application run. SMO changed the database name to DBA] instead of DBA. This works.
RESTORE DATABASE [DBA]] FROM DISK = N'C:\DBA-NoPassword.bak' WITH MOVE N'DBA' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\DBA.mdf', MOVE N'DBA_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\DBA_log.ldf', UNLOAD, REPLACE, STATS = 1
Trying to change the logical names, post restore.
Corrected SQL statement that works:
ALTER DATABASE [DBA]] MODIFY FILE(Name = [DBA], NEWNAME = [DBA]])
Uncorrected statement that does not work:
ALTER DATABASE [DBA] MODIFY FILE(Name = [DBA], NEWNAME = [DBA])
As you can see, I had to add an extra "]" at the end of the database name "DBA" to make the statement work. It is as if the extra "]" is acting as an escape character for the final ].
My question is.. if SMO can correctly restore a database name which contains "" in the name, then a SMO operation should be able to change the logical names. I have been googling and have not been able to find SMO code to incorporate into my VB.NET app, and I do not want to create special conditions for each special character that may be used in a database name. I also do not want to limit the special characters either.
Did I miss something on my google search for how to change logical names using SMO?
Let me know if you guys have any ideas.
I am aware that when I create a new database, it will create logical names based upon the database name. When creating a new database via a restore, it uses the logical names embedded in the backup headers. I cannot override the default logical names during the restore, and I need the move option because the remote server I am restoring to does not contain the same file paths as the backup file.
My question is for already existing databases, that are being restored, which already have logical names different than the database name.
I think a workaround solution might be for me to first drop the database, create a new database, and then do a restore with move to option. Sure seems like a lot of work. Please let me know if I can change logical names with SMO...