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

51

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

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

我在答案部分找到了使用命令行的方法来完成这个任务。

是否有更好、更易用的方法来完成此操作?

5个回答

81

我发现了需要在命令行上运行的SqlLocalDB实用程序。

可以在

C:\Program Files\Microsoft SQL Server\110\Tools\Binn
或者
C:\Program Files\Microsoft SQL Server\120\Tools\Binn

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

SqlLocalDB.exe i

 info|i
  Lists all existing LocalDB instances owned by the current user
  and all shared LocalDB instances.

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

SqlLocalDB.exe i "MSSQLLocalDB"

info|i "instance name"
  Prints the information about the specified LocalDB instance.

28
要列出所有的localdb实例,可以查看vineel的答案
如果您想要使用 UI 列出您默认的 localdb 实例 的所有 数据库(可能无法在 SSMS2019 中使用),请参考以下步骤:
只需打开 SSMS 并连接到 (LocalDB)\MSSQLLocalDB。 现在,您将看到所有 LocalDB 实例
这至少适用于 SS2016。 enter image description here

1
谢谢 :-) 在 SSMS 2017 中适用,但在 SSMS 2019 中不适用。 - Nicks
6
这是不正确的,你只连接到了默认的MSSQLLocalDB实例(并列出了数据库)。这不会列出所有实例的名称! - Ronald
2
即使使用SSMS(SS2016),这完全是错误的,它只显示一个实例,即MSSQLLocalDB。可能会有更多的实例,例如V11.0,ProjectsV12,ProjectsV13。为什么这个答案得到了很多赞? - Dush

7

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

    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;
    }

7
在Visual Studio 2017中,SQL Server对象浏览器会显示所有的LocalDb实例。

谢谢。这在VS 2022中仍然有效。因此肯定有一种容易的方法,无需运行任何命令。 - Varun Sharma

4
Visual Studio 2019Server Explorer(或者那里的SQL Server Object Explorer按钮)中,点击“Add SQL Server”按钮。

Add SQL Server Button

展开本地(Local)选项卡,查看当前正在运行的本地SQL Server服务列表。只有在连接到所选服务器后,它才会出现在SQL Server对象资源管理器(SQL Server Object Explorer)中:

SQL Server Object Explorer


1
很好的解释,并给出了不同(以视觉方式找到)和正确的答案来回答OP的问题。 - Dush

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接