SQL Server:如何查找所有localdb实例名称

vin*_*eel 34 sql-server sql-server-2008 sql-server-2012

我的系统中安装了两个版本(2012,2014)的SQL Server Express LocalDB.

如何查找所有现有LocalDB实例名称?

我找到了一种使用命令行的方法,如答案部分所述.

有没有更好,更简单的方法呢?

vin*_*eel 53

我找到了需要在命令行上运行的SqlLocalDB实用程序.

可以在中找到SqlLocalDB

C:\Program Files\Microsoft SQL Server\110\Tools\Binn
Run Code Online (Sandbox Code Playgroud)

要么

C:\Program Files\Microsoft SQL Server\120\Tools\Binn
Run Code Online (Sandbox Code Playgroud)

要获取所有现有LocalDB实例名称,请使用:

SqlLocalDB.exe i

 info|i
  Lists all existing LocalDB instances owned by the current user
  and all shared LocalDB instances.
Run Code Online (Sandbox Code Playgroud)

要获取有关特定LocalDB实例的详细信息:

SqlLocalDB.exe i "MSSQLLocalDB"

info|i "instance name"
  Prints the information about the specified LocalDB instance.
Run Code Online (Sandbox Code Playgroud)


Leg*_*nds 13

如果您更喜欢UI方式

只需打开您的SSMS并连接即可(LocalDB)\MSSQLLocalDB.
现在您将看到所有的LocalDB实例.

这至少适用于SS2016,不能保证早期版本.

在此输入图像描述

  • 这是不正确的,您仅连接到默认的“MSSQLLocalDB”实例(并列出数据库)。这并未列出所有实例名称! (3认同)
  • 即使对于 SSMS (SS2016),这也是完全不正确的,它只显示一个 MSSQLLocalDB 实例。可能还有更多实例,例如 V11.0、ProjectsV12、ProjectsV13。为什么这个答案得到了很多赞同? (2认同)

小智 6

这是我用来从命令行获取所有实例的方法 -

    internal static List<string> GetLocalDBInstances()
    {
        // Start the child process.
        Process p = new Process();
        // Redirect the output stream of the child process.
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "/C sqllocaldb info";
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        p.Start();
        // Do not wait for the child process to exit before
        // reading to the end of its redirected stream.
        // p.WaitForExit();
        // Read the output stream first and then wait.
        string sOutput = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        //If LocalDb is not installed then it will return that 'sqllocaldb' is not recognized as an internal or external command operable program or batch file.
        if (sOutput == null || sOutput.Trim().Length == 0 || sOutput.Contains("not recognized"))
            return null;
        string[] instances = sOutput.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        List<string> lstInstances = new List<string>();
        foreach (var item in instances)
        {
            if (item.Trim().Length > 0)
                lstInstances.Add(item);
        }
        return lstInstances;
    }
Run Code Online (Sandbox Code Playgroud)


Ric*_*ane 5

在Visual Studio 2017中,SQL Server对象资源管理器将向您显示所有LocalDb实例


小智 5

Visual Studio 2019 服务器资源管理器(或SQL Server 对象资源管理器按钮)中,单击“添加 SQL Server ”按钮

添加 SQL Server 按钮

并展开“本地”选项卡以查看它找到的当前正在运行的本地SQL Server服务的列表。仅当您连接到所选服务器时,它才会在SQL Server 对象资源管理器中列出:

SQL Server 对象资源管理器