I’m creating a client application that connects to MS SQL Sever via ADO.NET. I need a user to specify a connection string to connect to a database. However users should only connect to SQL Server Express that is installed on their machine. I need to check the connection string to determine if it’s a local one or not.
The obvious idea is to check whether the CS starts with “.” or “local” or “*this_machine_name*”. But I’m not sure that covers all possible cases.
Is there a better way to perform such a check?
UPD: I imply that there may be more than one instance of the SQL Server on the local machine, and instance names are not known. There also may be an unnamed instance.
UPD2: In fact I only ask users to specify an SQLServer instance.
Thank you.
Retrieve the connection string, extract the hostname from it, pass the hostname to System.Net.Dns.GetHostEntry(String) and voíla! You get an IPHostEntry instance containing IP adresses. Check if 127.0.0.1 is there.