Search code examples
c#sql-serverrestoresmo

Restore SQL Server database failed SMO C#


I'm trying to restore a SQL-Server database but I don't know why it's throwing exception which message is "Restore failed for server '.'".

I'm executing two methods. One is defines as CheckDatabase and second one is RestoreDatabase. If I execute RestoreDatabase method first it works fine, but if I execute it after CheckDatabase method it explodes

This is my CheckDatabase():

    private static void CheckDatabase(string period)
    {
        string connString = Config.ConnectionStrings.ConnectionStrings["connString"].ConnectionString;
        string controlProcesoCommand = "select top 5 * from ControlProceso order by FechaInicio desc;";

        using (SqlConnection conn = new SqlConnection(connString))
        using (SqlCommand command = new SqlCommand(controlProcesoCommand, conn))
        {
            try
            {
                conn.Open();
                var reader = command.ExecuteReader();

                while (reader.Read())
                {
                    int lastPeriodDb = int.Parse(reader["Periodo"].ToString());
                    int actualPeriod = int.Parse(period);

                    if (period.Equals(reader["Periodo"].ToString()))
                        throw new Exception(Resources.Messages.Period_already_processed);
                    else if (lastPeriodDb > actualPeriod)
                        throw new Exception(Resources.Messages.Period_saved_after_actual_period);
                    else break;
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
                command.Cancel();
                command.Dispose();
            }
        }
    }

And this is my RestoreDatabase():

private static void RestoreDatabase()
    {
        try
        {
            SqlConnection conn = new SqlConnection(Config.ConnectionStrings.ConnectionStrings["connString"].ConnectionString);

            string dbName = conn.Database;
            string restoreFilePath = Config.AppSettings.Settings["RestoreFilePath"].Value;

            Server myServer = new Server(conn.DataSource);
            Database itauDatabase = new Database(myServer, dbName);


            Restore dbRestore = new Restore();
            dbRestore.Action = RestoreActionType.Database;
            dbRestore.Database = itauDatabase.Name;
            dbRestore.Devices.AddDevice(restoreFilePath, DeviceType.File);
            dbRestore.ReplaceDatabase = true;
            dbRestore.SqlRestore(myServer);

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Each method works fine when I execute them separated.

Thanks!


Solution

  • That error is pretty vague. Any inner exception that you can add? My guess is that after running CheckDatabase() there is still an open connection to the database which is blocking the restore. Maybe try force closing connections during the restore.