Merci beaucoup à Mitch pour la grande réponse qu'il met ensemble. Cependant, ce que je l'ai fait par la suite est comme ce qui suit:
J'ai deux méthodes distinctes pour obtenir instance de serveur local et à distance respectivement. Les instances locales sont extraites du registre. Vous devez rechercher les deux ruches de WOW64 et WOW3264 pour obtenir à la fois serveur SQL 2008 (64 bits) et SQL Server Express (32 bits)
est le code que j'utilise ici:
/// <summary>
/// get local sql server instance names from registry, search both WOW64 and WOW3264 hives
/// </summary>
/// <returns>a list of local sql server instance names</returns>
public static IList<string> GetLocalSqlServerInstanceNames()
{
RegistryValueDataReader registryValueDataReader = new RegistryValueDataReader();
string[] instances64Bit = registryValueDataReader.ReadRegistryValueData(RegistryHive.Wow64,
Registry.LocalMachine,
@"SOFTWARE\Microsoft\Microsoft SQL Server",
"InstalledInstances");
string[] instances32Bit = registryValueDataReader.ReadRegistryValueData(RegistryHive.Wow6432,
Registry.LocalMachine,
@"SOFTWARE\Microsoft\Microsoft SQL Server",
"InstalledInstances");
FormatLocalSqlInstanceNames(ref instances64Bit);
FormatLocalSqlInstanceNames(ref instances32Bit);
IList<string> localInstanceNames = new List<string>(instances64Bit);
localInstanceNames = localInstanceNames.Union(instances32Bit).ToList();
return localInstanceNames;
}
public enum RegistryHive
{
Wow64,
Wow6432
}
public class RegistryValueDataReader
{
private static readonly int KEY_WOW64_32KEY = 0x200;
private static readonly int KEY_WOW64_64KEY = 0x100;
private static readonly UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002;
private static readonly int KEY_QUERY_VALUE = 0x1;
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegOpenKeyEx")]
static extern int RegOpenKeyEx(
UIntPtr hKey,
string subKey,
uint options,
int sam,
out IntPtr phkResult);
[DllImport("advapi32.dll", SetLastError = true)]
static extern int RegQueryValueEx(
IntPtr hKey,
string lpValueName,
int lpReserved,
out uint lpType,
IntPtr lpData,
ref uint lpcbData);
private static int GetRegistryHiveKey(RegistryHive registryHive)
{
return registryHive == RegistryHive.Wow64 ? KEY_WOW64_64KEY : KEY_WOW64_32KEY;
}
private static UIntPtr GetRegistryKeyUIntPtr(RegistryKey registry)
{
if (registry == Registry.LocalMachine)
{
return HKEY_LOCAL_MACHINE;
}
return UIntPtr.Zero;
}
public string[] ReadRegistryValueData(RegistryHive registryHive, RegistryKey registryKey, string subKey, string valueName)
{
string[] instanceNames = new string[0];
int key = GetRegistryHiveKey(registryHive);
UIntPtr registryKeyUIntPtr = GetRegistryKeyUIntPtr(registryKey);
IntPtr hResult;
int res = RegOpenKeyEx(registryKeyUIntPtr, subKey, 0, KEY_QUERY_VALUE | key, out hResult);
if (res == 0)
{
uint type;
uint dataLen = 0;
RegQueryValueEx(hResult, valueName, 0, out type, IntPtr.Zero, ref dataLen);
byte[] databuff = new byte[dataLen];
byte[] temp = new byte[dataLen];
List<String> values = new List<string>();
GCHandle handle = GCHandle.Alloc(databuff, GCHandleType.Pinned);
try
{
RegQueryValueEx(hResult, valueName, 0, out type, handle.AddrOfPinnedObject(), ref dataLen);
}
finally
{
handle.Free();
}
int i = 0;
int j = 0;
while (i < databuff.Length)
{
if (databuff[i] == '\0')
{
j = 0;
string str = Encoding.Default.GetString(temp).Trim('\0');
if (!string.IsNullOrEmpty(str))
{
values.Add(str);
}
temp = new byte[dataLen];
}
else
{
temp[j++] = databuff[i];
}
++i;
}
instanceNames = new string[values.Count];
values.CopyTo(instanceNames);
}
return instanceNames;
}
}
SqlDataSourceEnumerator.Instance.GetDataSources() is used to get remote sql server instances.
À la fin, je fusionne juste la liste des instances distantes et la liste des instances locales pour produire le résultat final.
le studio visuel ne marche pas reconnaître la functrion: FormatLocalSqlInstanceNames (ref instances64Bit); FormatLocalSqlInstanceNames (ref instances32Bit); –
Que fait "FormatLocalSqlInstanceNames"? Pouvez-vous s'il vous plaît poster le code? – yaronkl