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


当前回答

SQL Server permits applications to find SQL Server instances within the current network. The SqlDataSourceEnumerator class exposes this information to the application developer, providing a DataTable containing information about all the visible servers. This returned table contains a list of server instances available on the network that matches the list provided when a user attempts to create a new connection, and expands the drop-down list containing all the available servers on the Connection Properties dialog box. The results displayed are not always complete. In order to retrieve the table containing information about the available SQL Server instances, you must first retrieve an enumerator, using the shared/static Instance property:

using System.Data.Sql;

class Program
{
  static void Main()
  {
    // Retrieve the enumerator instance and then the data.
    SqlDataSourceEnumerator instance =
      SqlDataSourceEnumerator.Instance;
    System.Data.DataTable table = instance.GetDataSources();

    // Display the contents of the table.
    DisplayData(table);

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
  }

  private static void DisplayData(System.Data.DataTable table)
  {
    foreach (System.Data.DataRow row in table.Rows)
    {
      foreach (System.Data.DataColumn col in table.Columns)
      {
        Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
      }
      Console.WriteLine("============================");
    }
  }
}

来自MSDN http://msdn.microsoft.com/en-us/library/a6t1z9x2(v=vs.80).aspx

其他回答

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

在命令行中:

SQLCMD -L

or

OSQL -L

(注:必须大写L)

这将列出网络上安装的所有sql server。您可以设置一些配置选项来防止SQL Server显示在列表中。要做到这一点…

在命令行:

svrnetcn

在启用的协议列表中,选择“TCP/IP”,然后单击属性。有一个“隐藏服务器”的复选框。

我知道这是一个老帖子,但我发现了一个很好的解决方案与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 2008,但我无法连接到任何数据库实例。 @G Mastros发布的命令没有列出活动实例。

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

你可以查询这个注册表值直接得到SQL版本:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\90\Tools\ClientSetup\CurrentVersion

或者你可以查询你的实例名,然后使用sqlcmd和你想要的实例名:

要查看实例名:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names

然后执行这个:

SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')

如果您使用c++,您可以使用这段代码来获取注册表信息。