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


当前回答

If you just want to see what's installed on the machine you're currently logged in to, I think the most straightforward manual process is to just open the SQL Server Configuration Manager (from the Start menu), which displays all the SQL Services (and only SQL services) on that hardware (running or not). This assumes SQL Server 2005, or greater; dotnetengineer's recommendation to use the Services Management Console will show you all services, and should always be available (if you're running earlier versions of SQL Server, for example).

If you're looking for a broader discovery process, however, you might consider third party tools such as SQLRecon and SQLPing, which will scan your network and build a report of all SQL Service instances found on any server to which they have access. It's been a while since I've used tools like this, but I was surprised at what they found (namely, a handful of instances that I didn't know existed). YMMV. You might Google for details, but I believe this page has the relevant downloads: http://www.sqlsecurity.com/Tools/FreeTools/tabid/65/Default.aspx

其他回答

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

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

当我评估100多个服务器时,我也遇到了同样的问题,我用c#写了一个脚本来浏览由SQL组成的服务名称。当在服务器上安装实例时,SQL server为每个实例添加一个服务,并使用服务名。对于不同的版本,例如2000年到2008年,它可能有所不同,但肯定有一个具有实例名的服务。

我获取服务名并从服务名获得实例名。下面是WMI查询结果的示例代码:

if (ServiceData.DisplayName == "MSSQLSERVER" || ServiceData.DisplayName == "SQL Server (MSSQLSERVER)")
            {
                InstanceData.Name = "DEFAULT";
                InstanceData.ConnectionName = CurrentMachine.Name;
                CurrentMachine.ListOfInstances.Add(InstanceData);
            }
            else
                if (ServiceData.DisplayName.Contains("SQL Server (") == true)
                {
                    InstanceData.Name = ServiceData.DisplayName.Substring(
                                            ServiceData.DisplayName.IndexOf("(") + 1,
                                            ServiceData.DisplayName.IndexOf(")") - ServiceData.DisplayName.IndexOf("(") - 1
                                        );
                    InstanceData.ConnectionName = CurrentMachine.Name + "\\" + InstanceData.Name;
                    CurrentMachine.ListOfInstances.Add(InstanceData);
                }
                else
                    if (ServiceData.DisplayName.Contains("MSSQL$") == true)
                    {
                        InstanceData.Name = ServiceData.DisplayName.Substring(
                                                ServiceData.DisplayName.IndexOf("$") + 1,
                                                ServiceData.DisplayName.Length - ServiceData.DisplayName.IndexOf("$") - 1
                                            );

                        InstanceData.ConnectionName = CurrentMachine.Name + "\\" + InstanceData.Name;
                        CurrentMachine.ListOfInstances.Add(InstanceData);
                    }

SQL Server浏览器服务http://msdn.microsoft.com/en-us/library/ms181087.aspx

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

或使用 SQLCMD - l

这个查询可以得到服务器名和实例名:

SELECT @@SERVERNAME, @@SERVICENAME