【问题标题】:SQL Server: How to find all localdb instance namesSQL Server:如何查找所有 localdb 实例名称
【发布时间】:2016-03-22 14:30:16
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: sql-server sql-server-2008 sql-server-2012


    【解决方案1】:

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

    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.
    

    【讨论】:

      【解决方案2】:

      要列出所有localdb instances,请查看下面的 vineel 的answer


      如果您想列出所有 databases 默认 localdb 使用 UI 的实例,请看这里可能不再与 SSMS2019 一起使用):

      只需打开您的 SSMS 并连接到 (LocalDB)\MSSQLLocalDB
      现在您将看到您的所有 LocalDB-Instances

      这至少适用于 SS2016。

      【讨论】:

      • 谢谢 :-) 在 SSMS 2017 中对我有用,而对 SSMS2019 无效
      • 这是不正确的,您只是连接到默认的 MSSQLLocalDB 实例(并列出数据库)。这并未列出所有实例名称!
      • 这完全不正确,即使使用 SSMS (SS2016) 它只显示一个实例是 MSSQLLocalDB。可能还有更多实例,例如 V11.0、ProjectsV12、ProjectsV13。为什么这个答案得到了很多赞成?
      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          Add SQL Server Button

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

          SQL Server Object Explorer

          【讨论】:

          • 对 OPs 问题给出了很好的解释并给出了不同的(以视觉方式找到)和正确的答案。
          猜你喜欢
          • 1970-01-01
          • 2015-01-27
          • 1970-01-01
          • 2015-06-03
          • 1970-01-01
          • 1970-01-01
          • 2010-09-12
          相关资源
          最近更新 更多