I want to achieve the following programatically using C#.
Query components installed in SQL Server 2008 e.g
I have tried using SMO API for SQL Server tried to query Information Collection as well Configuration collection but could not find the any of the above three points.
I need to be able to programmatically determine which SQL Server 2008 components are installed and which are not installed, just like SQL Server Installation setup which first checks to see which components are installed.
One way is to query SQL Server Installation Log File, but it is not helpful if SQL Server is completely no there or if someone has deleted that log file.
I need to reliably determine the installed SQL Server components
Please help.
Thanks
Steve
Your best bet is probably to query Win32_Product via WMI. You can use WMI Code Creator v1.0 to help you create an appropriate query. You can also see WQL (SQL for WMI) for information on the supported query syntax and ManagementObjectSearcher Class.
Something like this should do the trick:
using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
public class MyWMIQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_Product WHERE Name LIKE '%SQL%'");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_Product instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);
Console.WriteLine("InstallLocation: {0}", queryObj["InstallLocation"]);
Console.WriteLine("Name: {0}", queryObj["Name"]);
Console.WriteLine("SKUNumber: {0}", queryObj["SKUNumber"]);
Console.WriteLine("Vendor: {0}", queryObj["Vendor"]);
Console.WriteLine("Version: {0}", queryObj["Version"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
}
}
}
You can find out about installed features by running the SQL Server Setup Discovery Report (requires admin privileges):
You can also create the report from a command line by running setup.exe /ACTION=RUNDISCOVERY /Q
(from C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\SQLServer2008R2
in the case of SQL Server 2008 R2). The report files will typically be in C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log\YYYYMMDD_HHmmSS
, where YYYYMMDD_HHmmSS is the date and time the report was run. SqlDiscoveryReport.xml is probably the one most useful for consuming programmatically. Be aware that these locations can vary somewhat based on the install location the user chooses during the initial setup and the version of SQL Server.