I want to programmatically enumerate the SQL Server 2005 named instances on a server. I know I should be able to do this fairly easily, but I can't for the life of me figure out how. Thanks in advance for any help!
+1
A:
Here is what I had written a while ago...This is not exactly the same but it may give you an idea on how to do it. Here I am enumerating and checking for the express edition of the sql server, but you should be able to change that...You will need to go to the registry to find the SKUNAME and SKULEVEL of the sql server 2005.
public static bool IsSqlExpressPresent(string strInstance, Utility.Log log)
{
const string edition = "Express Edition";
string instance = "MSSQL$" + strInstance.ToUpper();
const int spLevel = 1;
bool fCheckEdition = false;
bool fCheckSpLevel = false;
try
{
// Run a WQL query to return information about SKUNAME and SPLEVEL about installed instances
// of the SQL Engine.
ManagementObjectSearcher getSqlExpress =
new ManagementObjectSearcher("root\\Microsoft\\SqlServer\\ComputerManagement",
"select * from SqlServiceAdvancedProperty where SQLServiceType = 1 and ServiceName = '"
+ instance + "' and (PropertyName = 'SKUNAME' or PropertyName = 'SPLEVEL')");
// If nothing is returned, SQL Express isn't installed.
if (getSqlExpress.Get().Count == 0)
{
return false;
}
// If something is returned, verify it is the correct edition and SP level.
foreach (ManagementObject sqlEngine in getSqlExpress.Get())
{
if (sqlEngine["ServiceName"].ToString().Equals(instance))
{
switch (sqlEngine["PropertyName"].ToString())
{
case "SKUNAME":
// Check if this is Express Edition or Express Edition with Advanced Services
fCheckEdition = sqlEngine["PropertyStrValue"].ToString().Contains(edition);
break;
case "SPLEVEL":
// Check if the instance matches the specified level
fCheckSpLevel = int.Parse(sqlEngine["PropertyNumValue"].ToString()) >= spLevel;
//fCheckSpLevel = sqlEngine["PropertyNumValue"].ToString().Contains(spLevel);
break;
}
}
}
if (fCheckEdition & fCheckSpLevel)
{
return true;
}
return false;
}
catch (ManagementException e)
{
if (log != null)
{
log.Write(
Utility.LogState.Error,
"Database",
"Could not find OfficeClip instance of SqlExpress: " + e.ErrorCode + ", " + e.Message
);
}
return false;
}
}
Samuel
2009-09-24 22:57:02
Perfect! Thanks.
GuyBehindtheGuy
2009-09-26 19:45:45