Search code examples
c#sql-serverdatabaserestore.net-4.6.1

Method not Found while restoring sql database C#


This is a sample of the code I'm working on, the main issue is I can't run the restore operation when running on PCs with SQL Server client 10.0.0.0. The process crashes on this line:

DataTable logicalRestoreFiles = res.ReadFileList(myServer);

and the exception thrown is:

Method not found Microsoft.SqlServer.Managment.Common.ServerConnection.GetDatabaseConnection(System.String)

This problem only happens when i try to run this code on PCs with SQL Server 2008 R2. I tried it on SQL Server 2017 and the database got restored without problems. I really need some help here I'm out of ideas.

Here are the current Connection packages and their versions in case it helps:

 <package id="Microsoft.Data.ConnectionUI" version="16.4.29519.181" targetFramework="net461" />
 <package id="Microsoft.Data.Tools.Sql.BatchParser" version="150.18097.0-xplat" 
  targetFramework="net461" />
 <package id="Microsoft.SqlServer.ConnectionInfo" version="150.18097.0-xplat" 
  targetFramework="net461" />
 <package id="Microsoft.SqlServer.ConnectionInfo.dll" version="1.0.1" targetFramework="net461" />
 <package id="Microsoft.SqlServer.Management.Sdk.Sfc.dll" version="1.0.1" targetFramework="net461" 
  />
 <package id="Microsoft.SqlServer.Smo.dll" version="1.0.1" targetFramework="net461" />
 <package id="Microsoft.SqlServer.SmoExtended.dll" version="1.0.1" targetFramework="net461" />
 <package id="Microsoft.SqlServer.SqlEnum.dll" version="1.0.1" targetFramework="net461" />
 <package id="System.Data.SqlClient" version="4.4.0" targetFramework="net461" />

public void RestoreDatabase(string DBName)
{
    ServerConnection connection = new ServerConnection(txtServidor.Text, txtUsuario.Text, txtContraseña.Text);
    Server myServer = new Server(connection);            

    Restore res = new Restore();
    res.Database = DBName;
    res.Action = RestoreActionType.Database;
    res.Devices.AddDevice(ConfigurationManager.AppSettings.Get("bakPathToFile"), DeviceType.File);            
    res.PercentCompleteNotification = 10;
    res.ReplaceDatabase = true;
    res.PercentComplete += new PercentCompleteEventHandler(res_PercentComplete);

    try
    {
        bool logicalNamesRetrieved = false;
        string mdfFile = "";
        string logFile = "";

        var Response = EnviarComando("SELECT top 1 physical_name FROM sys.database_files");

        if (Response.Success)
        {
            mdfFile = CommandContent.Replace("master.mdf",_databasename+".mdf");
            logFile = CommandContent.Replace("master.mdf", _databasename + "_Log.ldf");
        }

        if(String.IsNullOrEmpty(mdfFile) || String.IsNullOrEmpty(logFile))
        {
            Logger.WriteLog("Ocurrio un error al buscar la ruta del archivo mdf o ldf.","Error");
        }
        else
        {
            try
            {
                DataTable logicalRestoreFiles = res.ReadFileList(myServer);
                res.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[0][0].ToString(), mdfFile));
                res.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[1][0].ToString(), logFile));
                logicalNamesRetrieved = true;
            }
            catch (Exception ex)
            {
                Logger.WriteLog("Excepcion al buscar logical names de los archivos mdf y log "+ex.Message + ex.InnerException != null ?
                ex.InnerException.Message : "", "Error rutas");
            }
        }

        if(logicalNamesRetrieved) 
           res.SqlRestore(myServer);
    }
    catch (Exception ex)
    {
        Logger.WriteLog("Ocurrio un error al intentar restaurar la base de datos. " + ErrorMesage + ex.InnerException != null ?
        ex.InnerException.Message : "", "Error");
    }
}

Solution

  • You must ensure you that you are running the same SMO version on all PCs i.e. in this case .NET 4.6.1 should be installed everywhere you are running your 4.6.1 compiled code.

    Download link: https://support.microsoft.com/en-us/help/3102436/the-net-framework-4-6-1-offline-installer-for-windows

    Also, please note that Microsoft no longer supports SQL Server 2008 R2 so I strongly suggest you have those boxes upgraded.