Detect SQL Server 2008 R2
I've googled a day but the question is still question: how to detect SQL Server 2008 R2 on various Windows versions:
- via registry (would be our favorite solution),
- via file system,
- via installer exit code?
Installer developed with NSIS. Some additional informations:
Registry
Samples on the net are out of date or simply improper. Not just 开发者_如何学运维R2 but 2k8 detection is problematic too.
File system
I have no idea what files are especially from 2k8 R2.
Installer exit code
In some cases exits without error code (i.e. prerequisites missing).
you could use the WMI to listy all the microsoft product installed and then you could look foe the one you need
public static class MyClass
{
public static void Main()
{
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
foreach (ManagementObject mo in mos.Get())
{
Console.WriteLine(mo["Name"]);
}
}
}
try to execute this query:
SELECT @@Version
I get this result back:
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) Apr 2 2010 15:48:46 Copyright (c) Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 5.2 (Build 3790: Service Pack 2)
is this enough? For me yes :)
Look at the older SQLPing source code which has a variety of methods
And before you try execute query provided by Davide (SELECT @@Version) you can check, that MSSQL service is running
using System.ServiceProcess;
var list = ServiceController.GetServices().ToList();
if (list.Any(sc => sc.ServiceName.ToLower().Contains("mssql")))
I use below code in my application
SqlDataSourceEnumerator sqldatasourceenumerator1 = SqlDataSourceEnumerator.Instance;
DataTable datatable1 = sqldatasourceenumerator1.GetDataSources();
foreach (DataRow row in datatable1.Rows)
{
if (Environment.MachineName.Equals(row["ServerName"]))
{
isSqlServerPresent = true;
break;
}
}
The only issue is, this code works on when the machine is on network, but since in my case machine will be on network, so I was ok with this issue.
It gets me the local instance of SQL Server.
In fact if you want to list Servers in the network and SQL Servers instances on a machine or on the LAN, there are APIs for it.
There should be proper way to call NetServerEnum
Windows API, for examples see:
http://www.xtremevbtalk.com/showthread.php?t=107256
http://pinvoke.net/default.aspx/netapi32/netserverenum.html
How to detect SQL server instances / features installed on a machine
精彩评论