1

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");
    }
}
2
  • what is restore class? your own class or some package
    – Gauravsa
    Dec 31, 2019 at 12:58
  • 1
    @Gauravsa: most likely the class in the SMO (SQL Server Management Objects) library
    – marc_s
    Dec 31, 2019 at 13:12

2 Answers 2

1

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.

3
  • Hi! Happy new year, yes i double checked and the target pc has .net framework 4.6.1 installed, the same version. Could the problem be elsewhere? or am i lokking at this the wrong way? Thanks for your answer Jan 2, 2020 at 13:29
  • In that case, for some reason it still looks like the wrong version of the SMO dlls are loaded on the target pc. You could use learn.microsoft.com/en-us/sysinternals/downloads/procmon to pinpoint what is loaded exactly. Alternatively (and in my experience this proved to be a very quick and dirty but effective solution on some occasions) you could install the latest SQL Server Management Studio on the target pc as this contains many of the needed SMO references. But as said, it is a rather ugly solution so I'd suggest you investigate with procmon first.
    – Thailo
    Jan 2, 2020 at 13:45
  • Is there a way to config my app to use the smo Version i have referenced. And not use the one installed on the target pc? Jan 8, 2020 at 15:19
0

I'll post this alternate solution to the vanishing GetDatabaseConnection problem here, in case anyone else runs into mysterious error. I encountered it after upgrading a working VB.Net project from .Net Framework 4.0 to 4.8, both of which targeted a SQL Server 2014 instance on the same box.

I fixed this only after discovering in the Properties Window that the runtime version for the dlls the O.P. linked to, such as Microsoft.SqlServer.ConnectionInfo, SqlServer.Smo,SqlServer.SmoExtended, SqlEnum and SqlClient, were all set to v2.0.50727.

The runtime versions for all of thee files were changed to v4.0.30319 after I upgraded the SMO dll's to the latest (Sept. 30, 2020) Microsoft.SqlServer.SqlManagementObjects Nuget package + its dependencies. After this, the GetDatabaseConnection method reappeared.

Hopefully this will spare someone else the same process of trial & error, which involved a lot failed workarounds based on juggling .Net Framework versions, file References, Nuget packages and other such plunges into "DLL Hell."

3
  • Cannot really see from your answer what you actually did to fix the issue. What did you change?
    – Kappacake
    Aug 12, 2022 at 10:55
  • 1
    @demonicdaron "I upgraded the SMO dll's to the latest (Sept. 30, 2020) Microsoft.SqlServer.SqlManagementObjects Nuget package + its dependencies. After this, the GetDatabaseConnection method reappeared" Aug 12, 2022 at 14:26
  • 1
    in our case this problem occurred after upgrading the net framework version from 4.6.1 to 4.7.2. I've realised that at least one of the packages that were causing the error only support up to 4.6.2. We simply gave up on upgrading the framework and left if with the old packages (which work fine for our setup) and will re-write the app in due course
    – Kappacake
    Aug 12, 2022 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.