-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkTest.cs
49 lines (43 loc) · 1.23 KB
/
NetworkTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using System.Net.NetworkInformation;
namespace MySQLCLRFunctions
{
public static class NetworkTest
{
[SqlFunction()]
public static SqlBoolean Ping(SqlString Machine)
{
string host = Machine.ToString();
if (Machine.ToString().Contains("\\"))
{
host = Machine.ToString().Substring(0, Machine.ToString().IndexOf("\\") - 1);
// Named instance
}
if (PingHost(host))
return SqlBoolean.True;
else
return SqlBoolean.False;
}
private static bool PingHost(string nameOrAddress)
{
bool pingable = false;
Ping pinger = null;
try
{
pinger = new Ping();
PingReply reply = pinger.Send(nameOrAddress);
pingable = reply.Status == IPStatus.Success;
}
catch (PingException)
{
// Discard PingExceptions and return false;
}
finally
{
pinger?.Dispose();
}
return pingable;
}
}
}