[RESOLVED] Get the Server name from DNS name
Hi,
I need to obtain the sql server name of a server based on its DNS name.
I have looked at the system.net methods
Code:
servername = Dns.GetHostEntry("ReportServer")
servername = Dns.GetHostByName("ReportServer")
But they return ReportServer.Group.Local rather than the sql server name SQL0001.
Is there another method I could try?
Re: Get the Server name from DNS name
1. Add a reference to Microsoft.SqlServer.Smo to your project
2. Call the EnumAvailableSqlServers method to get available SQL servers.
Code:
Dim serverName As String = "ReportServer"
Dim sqlServers As DataTable = Microsoft.SqlServer.Management.Smo.SmoApplication.EnumAvailableSqlServers(False)
For Each row As DataRow In sqlServers.Rows
If row("Server").ToString.ToLower = serverName.ToLower Then
MessageBox.Show(row("Name").ToString)
End If
Next
This method call will take a few seconds to complete depending on how large your network is and since it is a blocking method, you might want to run it using a background thread if you want the UI stays responsive.
Re: [RESOLVED] Get the Server name from DNS name
Thanks that worked, after I finally figured out I actually had to reference it rather than just imports. It is not ideal being a little slow but it seems the best alternative.
The only other idea was to actually create a connection to the server then get the servername from the current connection.