且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何ping存储在SQL数据库中的不同IP的列表,并使用C#代码将结果存储在同一个数据库表中

更新时间:2021-07-23 22:23:10

首先,因为你可能只有几个地址要ping,一个接一个地检查他们会消耗很多时间。 Paralleism适合你。请参阅此文章: http://www.aspsnippets.com /Articles/Parallelism---Parallel-Task-Execution-in-.Net-4.0.aspx [ ^ ]



另一方面,asp.net和网络一般是无国籍的。只要没有人要求任何东西,Yor web applicatin就会睡着了。因此,在Web应用程序中执行计划任务实际上并不是一个好方法 - 如果托管在常规Web服务器(如IIS)中。但是你手头也有WebAPI,你可以在你自己的Windows服务中托管你的web服务(参见: ASP.NET WebAPI托管技术 [ ^ ])。



所以你要么是

A)制作一个简单的Windows服务来ping以指定的时间间隔主机,使用IIS作为Web服务器

B)使用Windows任务调度程序调用您的控制台应用程序,并使用IIS作为Web服务器

C)制作一个执行ping操作的Windows服务,同时提供Web服务。



这个最新版本是最复杂的,但它是最容易管理的产品。
First of all, as you will probably have more than just a few addresses to ping, checkig them one after the other would consume much time. Paralleism is just for you. See this article: http://www.aspsnippets.com/Articles/Parallelism---Parallel-Task-Execution-in-.Net-4.0.aspx[^]

On the other hand, asp.net and the web in general is stateless. Yor web applicatin will be asleep as long as nobody is requesting anything from it. Thus doing scheduled tasks in a web application is not really a good a good approach - if hosted in a regular web server, like IIS. But you have also WebAPI at hands and you can host your web service inside your own windows service for example (see: ASP.NET WebAPI Hosting Techniques[^]).

So you either
A) make a simple windows service to ping thos host at specified intervals, ans use IIS as web server
B) use windows task scheduler to call your console application which does the same, and use IIS as web server
C) make a windows service that does the ping, and in the same time it provides the web service.

This latest one is the most complicated to make, but it is the easiest to manage as a product.


由于您已经拥有单个IP的代码,您只需运行一个包含所有IP的循环即可数据库。



因此,从数据库表中获取IP并运行循环。在循环内部,就像使用IP一样。此外,在获得结果后,请根据您的逻辑连接数据库并存储该特定IP的结果。
As you already have the code for a single IP, you just need to run a loop with all the IPs you get from the database.

So, get the IPs from the database table and run a loop. Inside the loop do as you are doing with the IP. Also, just after you get the result, connect with the database and store the result for that particular IP according to your logic.