我试图确定我已经安装的sql server/sql express的实例(手动或编程),但所有的例子都告诉我运行一个sql查询来确定这假设我已经连接到一个特定的实例。


当前回答

我也有同样的问题。“osql -L”命令只显示服务器列表,但不显示实例名(只显示本地SQL server的实例)。 通过Wireshark, sqlbrowser.exe(可以在SQL安装的共享文件夹中找到),我找到了解决我的问题的方法。

本地实例由注册表项解析。远程实例通过UDP广播(端口1434)和SMB进行解析。 使用"sqlbrowser.exe -c"列出请求。

My configuration uses 1 physical and 3 virtual network adapters. If I used the "osql -L" command the sqlbrowser displayed a request from one of the virtual adaptors (which is in another network segment), instead of the physical one. osql selects the adpater by its metric. You can see the metric with command "route print". For my configuration the routing table showed a lower metric for teh virtual adapter then for the physical. So I changed the interface metric in the network properties by deselecting automatic metric in the advanced network settings. osql now uses the physical adapter.

其他回答

我刚刚安装了Sql server 2008,但我无法连接到任何数据库实例。 @G Mastros发布的命令没有列出活动实例。

因此,我查看了服务,发现SQL server代理被禁用。我把它设置为自动,然后再启动。

我知道这是一个老帖子,但我发现了一个很好的解决方案与PoweShell,你可以找到SQL实例安装在本地或远程机器上,包括版本,也可以扩展获得其他属性。

$MachineName = ‘.’ # Default local computer Replace . with server name for a remote computer

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘LocalMachine’, $MachineName)
$regKey= $reg.OpenSubKey("SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL" )
$values = $regkey.GetValueNames()
$values | ForEach-Object {$value = $_ ; $inst = $regKey.GetValue($value); 
              $path = "SOFTWARE\\Microsoft\\Microsoft SQL Server\\"+$inst+"\\MSSQLServer\\"+"CurrentVersion";
              #write-host $path; 
              $version = $reg.OpenSubKey($path).GetValue("CurrentVersion");
                          write-host "Instance" $value;
              write-host  "Version" $version}

将获得SQL server的实例 reg查询“HKLM\软件\Microsoft\Microsoft SQL Server\实例名称\SQL”

或使用 SQLCMD - l

另一个选择是运行SQLSERVER发现报告,进入SQLSERVER安装介质,双击setup.exe

在下一个屏幕中,转到工具并单击如下所示的发现报告

这将向你展示所有的实例以及整个功能..下面是我电脑上的快照

在Windows命令行中输入:

SC \\server_name query | find /I "SQL Server ("

其中“server_name”是希望在其上显示SQL实例的任何远程服务器的名称。

当然,这需要足够的权限。