C# Ping多个IP地址并将计时器设置为重新Ping
我在下面有ping IP地址的代码,但是当我运行此代码时,它没有运行,因为数据库中没有更新 以下是C# Ping多个IP地址并将计时器设置为重新Ping,c#,C#,我在下面有ping IP地址的代码,但是当我运行此代码时,它没有运行,因为数据库中没有更新 以下是开始代码: protected void Start() { string ip_STCKL = "*.*.10."; string ip_STCKCH = "*.*.128."; for (int ipCount = ip_addr; ipCount < 195; ipCount++) { string ip = ip_STCKL + ipC
开始
代码:
protected void Start()
{
string ip_STCKL = "*.*.10.";
string ip_STCKCH = "*.*.128.";
for (int ipCount = ip_addr; ipCount < 195; ipCount++)
{
string ip = ip_STCKL + ipCount.ToString();
string loopIp = ip;
WaitCallback func = delegate(object state)
{
if (PingIP(loopIp))
{
UpdateStatusKL(loopIp, true);
Console.WriteLine("Ping Success");
}
else
{
loopIp = ip_STCKCH + ipCount.ToString();
if (PingIP(loopIp))
{
UpdateStatusKL(loopIp, true);
//Console.WriteLine("Ping Success");
}
else
{
UpdateStatusKL(loopIp, false);
//Console.WriteLine("Ping Failed");
}
}
};
ThreadPool.QueueUserWorkItem(func);
//ThreadPool.GetAvailableThreads;
}
//Console.ReadLine();
}
获取状态后,更新到数据库中:
public void UpdateStatusKL(string IP, bool Status)
{
string New_Status = "";
if (Status)
{
New_Status = "REACHABLE";
}
else
{
New_Status = "UNREACHABLEE";
}
//Declare the connection object
OracleConnection Conn = new OracleConnection("Data Source=COMMSERVERKL;User
Id=mt_mon;Password=butus123");
//Make the connection
Conn.Open();
//Define you query
string sql = "UPDATE BalaiConnectionStatus SET Balai_Status = :pstatus,
Timestamp = :pTime WHERE IP_STCKL = :pIP OR IP_STCKCH = :pIP";
//Declare the Command
OracleCommand cmd = new OracleCommand(sql, Conn);
//Add the parameters needed for the SQL query
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("pstatus", OracleType.VarChar).Value = New_Status;
cmd.Parameters.Add("pTime", OracleType.VarChar).Value =
DateTime.Now.ToString();
cmd.Parameters.Add("pIP", OracleType.VarChar).Value = IP;
//Execute the query
cmd.ExecuteNonQuery();
Conn.Close();
}
计时器触发时,每隔15分钟,它将调用Start
功能再次开始ping所有IP地址:
private void timer1_Tick(object sender, EventArgs e)
{
ip_addr = 10;
Start();
}
我的代码有什么问题,我如何解决它?ExecuteOnQuery返回什么?尝试更改
cmd.Parameters.Add到cmd.Parameters.AddWithValue
相反,初始更新查询会产生什么结果(如果有的话)?您是否单独对各个方法进行单元测试,以确保它们以前可以工作处理整个应用程序?请注意,为什么不在数据库中存储一个状态位而不是整个字符串呢?Ofiris,谢谢您的回复。ExecuteOnQuery返回0.DJ KRAZE,结果仍然相同。没有更新。
private void timer1_Tick(object sender, EventArgs e)
{
ip_addr = 10;
Start();
}