我想以编程方式枚举服务器上的SQL Server 2005命名实例。我知道我应该能够相当容易地做到这一点,但我不能为我的生活弄清楚如何。在此先感谢您的帮助!
答案 0 :(得分:1)
答案 1 :(得分:1)
这是我刚才写的内容......这不完全相同,但它可能会让你知道如何做到这一点。这里我列举并检查sql server的快速版本,但你应该能够改变它...你需要去注册表找到sql server 2005的SKUNAME和SKULEVEL。
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;
}
}