Detecting a local SQL server installation using C # (32-bit as well as 64-bit)

How to determine if SQL is installed on the local computer using C #? Can I check the remote computer?

+3
source share
8 answers

You have several ways to do this:

  • SmoApplication.EnumAvailableSqlServers ()
  • SqlDataSourceEnumerator.Instance
  • Direct access to the registry

Direct access is not a recommended MS solution because they can change keys / paths. But other solutions do not provide instances on 64-bit platforms.

SQL Server . , x86 x64. Windows 64- . RegistryView ( .NET 4) .

using Microsoft.Win32;

RegistryView registryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
{
    RegistryKey instanceKey = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
    if (instanceKey != null)
    {
        foreach (var instanceName in instanceKey.GetValueNames())
        {
            Console.WriteLine(Environment.MachineName + @"\" + instanceName);
        }
    }
}

32- 64- ( , ), :

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server
+4

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\InstalledInstances

, , . how-to-get-sql-server-installation-path-programatically

. MSDN: SQL Server , SQL Server.

+2

Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion", ", "0.0.0.0");
+2

System.Management, SQL Server , .

+1

SQL 2005. .: Sqlserver2008 SMO, ?

using Microsoft.SqlServer.Management.Smo;

DataTable dt = SmoApplication.EnumAvailableSqlServers(true);
         string[] szSQLInstanceNames = new string[dt.Rows.Count];
         StringBuilder szSQLData = new StringBuilder();

         if (dt.Rows.Count > 0)
         {
            int i = 0;
            foreach (DataRow dr in dt.Rows)
            {
               try
               {
                  szSQLInstanceNames[i] = dr["Name"].ToString();
                  Server oServer;
                  oServer = new Server(szSQLInstanceNames[i]);
                  if (string.IsNullOrEmpty(dr["Instance"].ToString()))
                  {
                     szSQLInstanceNames[i] = szSQLInstanceNames[i] + "\\MSSQLSERVER";
                  }
                  szSQLData.AppendLine(szSQLInstanceNames[i] + "  Version: " + oServer.Information.Version.Major + "  Service Pack: " + oServer.Information.ProductLevel + "  Edition: " + oServer.Information.Edition + "  Collation: " + oServer.Information.Collation);
               }
               catch (Exception Ex)
               {
                  szSQLData.AppendLine("Exception occured while connecting to " + szSQLInstanceNames[i] + " " + Ex.Message);
               }

               i++;
            }

. , ​​ , .

oServer; oServer = (Environment.MAchineName);

, SQL.

+1

, . ( ), .

using System;
using System.Collections.Generic;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;

namespace Info.Data.Engine.SQLServer
{
  public static class SQLServerHelper
  {
    public static List<String> EnumerateServers()
    {
      var instances = SqlDataSourceEnumerator.Instance.GetDataSources();
      if ((instances == null) || (instances.Rows.Count < 1)) return null;

      var result = new List<String>();
      foreach (DataRow instance in instances.Rows)
      {
        var serverName = instance["ServerName"].ToString();
        var instanceName = instance["InstanceName"].ToString();
        result.Add(String.IsNullOrEmpty(instanceName) ? serverName : String.Format(@"{0}\{1}", serverName, instanceName));
      }
      return result;
    }

    public static List<String> EnumerateDatabases(String connectionString)
    {
      try
      {
        using (var connection = new SqlConnection(connectionString))
        {
          connection.Open();
          var databases = connection.GetSchema("Databases");
          connection.Close();
          if ((databases == null) || (databases.Rows.Count < 1)) return null;

          var result = new List<String>();
          foreach (DataRow database in databases.Rows)
          {
            result.Add(database["database_name"].ToString());
          }
          return result;
        }
      }
      catch
      {
        return null;
      }
    }
  }
}

,

+1

. , . , , .

0

Dejan Stanič.
:

, SQL Server 2008 .net sqlclient

0

Source: https://habr.com/ru/post/1714202/


All Articles