【发布时间】:2016-03-22 14:30:16
【问题描述】:
我的系统中安装了两个版本的 SQL Server Express LocalDB(2012、2014)。
如何找到所有现有的LocalDB 实例名称?
如答案部分所述,我找到了一种使用命令行的方法。
有没有更好更简单的方法?
【问题讨论】:
标签: sql-server sql-server-2008 sql-server-2012
我的系统中安装了两个版本的 SQL Server Express LocalDB(2012、2014)。
如何找到所有现有的LocalDB 实例名称?
如答案部分所述,我找到了一种使用命令行的方法。
有没有更好更简单的方法?
【问题讨论】:
标签: sql-server sql-server-2008 sql-server-2012
我找到了需要在命令行上运行的 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.
【讨论】:
要列出所有localdb instances,请查看下面的 vineel 的answer!
如果您想列出所有 databases 默认 localdb 使用 UI 的实例,请看这里(可能不再与 SSMS2019 一起使用):
只需打开您的 SSMS 并连接到 (LocalDB)\MSSQLLocalDB。
现在您将看到您的所有 LocalDB-Instances。
这至少适用于 SS2016。
【讨论】:
MSSQLLocalDB 实例(并列出数据库)。这并未列出所有实例名称!
这是我用来从命令行获取所有实例的方法 -
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;
}
【讨论】:
在 Visual Studio 2017 中,SQL Server 对象资源管理器将显示所有 LocalDb 实例
【讨论】:
在 Visual Studio 2019 服务器资源管理器(或 SQL Server 对象资源管理器按钮)点击“添加 SQL Server”按钮
并展开 Local 选项卡以查看它找到的当前正在运行的本地 SQL Server 服务的列表。只有当您连接到所选服务器时,它才会在 SQL Server 对象资源管理器中列出:
【讨论】: