Can't restore database with LocalDB
I have been using this code for about 2 years to restore 2008 r2 backup to
2008 r2 and it works every time. I am now trying to use the same code to
restore from 2008 r2 backup to 2012 localdb and it fails every time. The
error message is below. Not only does the restore fail, it leaves my
current database in 'recovery pending' state, and is unusable. I am using
c# and this must all be done programmatically with no user or DBA
involvement. Can you help me get this thing back on track?
using(var connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = String.Format("Alter Database {0} SET
SINGLE_USER With ROLLBACK IMMEDIATE", dbName);
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
connection.Open();
cmd.ExecuteNonQuery();
SqlCommand cmd2 = new SqlCommand();
cmd2.CommandText = String.Format(@"RESTORE DATABASE {0} FROM
DISK = '{1}'", dbName, backupFilePath);
cmd2.CommandType = CommandType.Text;
cmd2.Connection = connection;
cmd2.ExecuteNonQuery();
}
System.Data.SqlClient.SqlException (0x80131904): The logical database file
'MyData_log' cannot be found. Specify the full path for the file. RESTORE
could not start database 'MyData'. RESTORE DATABASE is terminating
abnormally.
No comments:
Post a Comment