H
Hans
Hi!
I'm trying to use SMO from C# to backup and restore (restore over an
existing database) a SQL-server database (SQL-server 2005). Everything is
working but when I add a few lines of code where I try to read the datafile
information which I will use to re-locate data files during the restore I
receive an error message.
Database db = svr.Databases[destinationDBName];
foreach (FileGroup group in db.FileGroups)
foreach (DataFile file in group.Files)
Console.WriteLine(file.FileName);
I receive the following error
{"System.Data.SqlClient.SqlError: RESTORE cannot process database 'MYTESTDB'
because it is in use by this session. It is recommended that the master
database be used when performing this operation."}
The error message is not shown when the code above is executed but when I do
the actual restore (res.SqlRestore(svr). However if I comment the 4 lines
of code above everything works just fine.
I tried to set the master as initial catalog but with no success (same error
message). As soon as I touch FileGroups/Files the database seems to be
locked for some reason.
Version of SMO assembly
Runtime version: v2.0.50727
Version: 10.0.0.0
Regards
/Hans
P.s I tried the sqlserver.programming group but did not get any replies
there so I try here D.s
I'm trying to use SMO from C# to backup and restore (restore over an
existing database) a SQL-server database (SQL-server 2005). Everything is
working but when I add a few lines of code where I try to read the datafile
information which I will use to re-locate data files during the restore I
receive an error message.
Database db = svr.Databases[destinationDBName];
foreach (FileGroup group in db.FileGroups)
foreach (DataFile file in group.Files)
Console.WriteLine(file.FileName);
I receive the following error
{"System.Data.SqlClient.SqlError: RESTORE cannot process database 'MYTESTDB'
because it is in use by this session. It is recommended that the master
database be used when performing this operation."}
The error message is not shown when the code above is executed but when I do
the actual restore (res.SqlRestore(svr). However if I comment the 4 lines
of code above everything works just fine.
I tried to set the master as initial catalog but with no success (same error
message). As soon as I touch FileGroups/Files the database seems to be
locked for some reason.
Version of SMO assembly
Runtime version: v2.0.50727
Version: 10.0.0.0
Regards
/Hans
P.s I tried the sqlserver.programming group but did not get any replies
there so I try here D.s