How can I determine is it local connection (localhost or 127.0.0.1) or is it remote connection (other machine in local area) if I have SqlConnection object?
You can get the connection string out of the SqlConnection obejct.
string s = connection.ConnectionString;
and check the data source or the server element of that string.
Edit: Code sample provided.
I think this function should work (not tested anyways).
private bool CheckConnectionStringLocalOrRemote(string connectionString) {
//Local machine
IPHostEntry entry = Dns.GetHostByAddress("127.0.0.1");
IPAddress[] addresses = entry.AddressList;
String[] aliases = entry.Aliases;
string hostName = entry.HostName;
if(connectionString.Contains(hostName))
return true;
foreach (IPAddress address in addresses) {
if (connectionString.Contains(address.ToString())) {
return true;
}
}
foreach (string alias in aliases) {
if (connectionString.Contains(alias))
return true;
}
return false;
}
Ps: make sure to add a using statement to System.Net namespace.