SQL2012 LocalDB: how to check in c# if it is currently installed?

帅比萌擦擦* 提交于 2019-11-27 01:33:47

问题


How to check in c# code if LocalDB currently installed? also, how to check if SQLNCLI11 presents in system?


回答1:


I found this nuget package that wraps up working with SQLLocalDB Has the following command

SqlLocalDbApi.IsLocalDBInstalled()



回答2:


Check if LocalDB is installed, by looking for this registry key:

[HKLM\SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions\11.0]

SQLNCLI11 - check the file version and presence of this file: C:\WINDOWS\system32\sqlncli.dll




回答3:


I'm using the answer to this question to check for the existence of sqllocaldb.exe

Like so:

public static bool IsLocalDBInstalled()
{
    return ExistsOnPath("SqlLocalDB.exe"); ;
}

public static bool ExistsOnPath(string fileName)
{
    return GetFullPath(fileName) != null;
}

public static string GetFullPath(string fileName)
{
    if (File.Exists(fileName))
        return Path.GetFullPath(fileName);

    var values = Environment.GetEnvironmentVariable("PATH");
    foreach (var path in values.Split(';'))
    {
        var fullPath = Path.Combine(path, fileName);
        if (File.Exists(fullPath))
            return fullPath;
    }
    return null;
}



回答4:


Here is a VB.NET example checking for LOCALDB

Public Shared Function CheckLocalDBExists() As Boolean
    Dim s As String = ""
    Dim reg As RegistryKey
    Dim rtn As Boolean = False
    reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions\12.0", True)
    Try
        s = reg.GetValue("ParentInstance", "").ToString
        reg.Close()
    Catch ex As Exception
        s = Nothing
    End Try
    'MessageBox.Show(s)
    If s = "MSSQL12E.LOCALDB" Then
        rtn = True
    End If
    Return rtn
End Function


来源:https://stackoverflow.com/questions/11628316/sql2012-localdb-how-to-check-in-c-sharp-if-it-is-currently-installed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!