2009-09-24 6 views

Répondre

1

Voici ce que j'avais écrit tout à l'heure ... Ce n'est pas exactement la même chose, mais il peut vous donner une idée sur la façon de le faire. Ici, j'énumère et vérifie l'édition expresse du serveur sql, mais vous devriez être en mesure de changer cela ... Vous devrez aller au registre pour trouver le SKUNAME et SKULEVEL du serveur sql 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; 
    } 
} 
+0

Parfait! Merci. – GuyBehindtheGuy

Questions connexes