简体   繁体   中英

I'm trying to detect that SQL Server Compact Edition is installed in a system or not

Same code is behaving differently in a console app & WPF app.

When I call the method in a WPF app, it returns true, even though SQL Server CE is not installed; however, the same code run in a console app returns false.

I found a way but it seems two behave from same code, one in console app & one in WPF app! What's making the difference?

I have logged each steps, shocked to see how it returns true in WPF

public static bool IsSQLCeV40Installed()
{
        var result = false;

        try
        {
            System.Reflection.Assembly.Load("System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
            result = true;
            _logger.Info($"IsSQLCeV40Installed: IsSqlCe {result}");
        }
        catch (System.IO.FileNotFoundException)
        {
            _logger.Info("IsSQLCeV40Installed: File Not Found Exception");
        }
        catch (Exception)
        {
            _logger.Info("IsSQLCeV40Installed: General Exception");
        }

        return result;
}

WPF prints this:

IsSQLCeV40Installed: IsSqlCe & returns true

Console app logs this:

IsSQLCeV40Installed: File Not Found Exception & returns false

However both should return as console app is doing in case that SQL Server CE is not installed on the machine!

One of the apps are most likely built for x86 and the other AnyCPU or x64, and therefore they look in either the 32 bit .NET Framework GAC or the 64 bit .NET Framework GAC.

More tips here: http://erikej.blogspot.com/2010/05/how-to-detect-if-x64-sql-compact-35-sp2.html

If your application relies on sql ce being there and a specific version then you can just copy the dll to the same location as your exe and then there's no need to install them. Thus no need to check if they're already installed.

Do that with whatever installer or disctribution method you use for an app. This is just an filecopy/xcopy rather than an install and will of course work fine with pushing file copy installs from a windows server.

If you have many applications using sql ce this would of course be somewhat wasteful of disk space on the client computer. This is not usually a concern. I think it's under 5 meg.

Generally, I would also advise you try and avoid driving programme logic by error handling. Where you can possibly avoid a try catch driving routine logic then do so. In this case you could check whether there is one of several possible entries in the user's registry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM