views:

366

answers:

2

The Windows default instalation comes with MSSQL client has a cliconfg application that is used to set aliases to Servers IP addresses and ports. Sometimes we use in our ConnectionStrings some of those aliases instead of IP address,port. How can I programatically translate that alias to an address using .NET?

Update: .NET + Windows automatically do this translation when we use the ConnectionString, but I don't know if the result of the translation (IP Address) is written in any public property where we can read it.

Update: I discovered that the cliconfg stores the aliases in the Windows Registry. I wonder if the Windows Registry is accessible from .NET

32bit HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo

64bit HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo
A: 

As long as you don't need the port, it's fairly trivial to get the IP address using the System.Net.Dns class :

IPAddress[] ips = Dns.GetHostAddresses("myHostName")
inferis
Thanks inferis. But this class of Alias isn't DNS resolvable. The method you suggested threw an SocketException "This host is unknown".
Jader Dias
Fair enough, I guess I didn't entirely understand the issue. You've given me something to read up on now :)
inferis
+1  A: 

I found the following solution for 32 bit versions of Windows:

using Microsoft.Win32;

static void Main(string[] args)
{
        var Aliases = Registry.LocalMachine.OpenSubKey(
      @"SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo");
        Aliases.GetValue("Alias");
}
Jader Dias