小Lee闲暇时间钻牛角尖,写了一个不操作IP字符串的遍历IP段并返回活动IP列表的方法,闲话就不说了,直接贴CODE

IPhost 类
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Diagnostics;
namespace Test2._0
{
class IPhost
{
public IPhost()
{
}
/// <summary>
/// 获取IP段中的活动IP列表
/// </summary>
/// <param name="startIP">开始IP</param>
/// <param name="endIP">结束IP</param>
/// <returns></returns>
public List<IPAddress> GetLiveIP(IPAddress startIP, IPAddress endIP)
{
List<IPAddress> ipList = new List<IPAddress>();
IPAddress currentIP = new IPAddress(startIP.Address);
currentIP = startIP;
while (currentIP.Address <= endIP.Address)
{
if (getIpState(currentIP) == "连接")
{
ipList.Add(currentIP);
}
currentIP = NextIP(currentIP);
}
return ipList;
}
/// <summary>
/// 从一个IPAddress 获取它的下一个 IPAddress
/// </summary>
/// <param name="theIP">IPAddress</param>
/// <returns></returns>
public IPAddress NextIP(IPAddress theIP)
{
uint startuint = (uint)IPAddress.NetworkToHostOrder((int)theIP.Address);
long NetWorklong = (long)IPAddress.HostToNetworkOrder((int)(startuint + 1));
return new IPAddress(NetWorklong);
}
/// <summary>
/// 判断IP是否活动
/// </summary>
/// <param name="theIP"></param>
/// <returns></returns>
public bool IpIsLive(IPAddress theIP)
{
System.Net.NetworkInformation.Ping pi = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply pr = pi.Send(theIP);
if ((pr.Status & System.Net.NetworkInformation.IPStatus.Success) == System.Net.NetworkInformation.IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 用cmd 判断IP状态 (古老的方法,放着纪念)
/// </summary>
/// <param name="theIP"></param>
/// <returns></returns>
public string getIpState(IPAddress theIP)
{
Process pcs = new Process();
pcs.StartInfo.FileName = "cmd.exe";
pcs.StartInfo.UseShellExecute = false;
pcs.StartInfo.RedirectStandardInput = true;
pcs.StartInfo.RedirectStandardOutput = true;
pcs.StartInfo.RedirectStandardError = true;
pcs.StartInfo.CreateNoWindow = true;
string pingStr;
pcs.Start();
pcs.StandardInput.WriteLine("ping -n 1 " + theIP.ToString());
pcs.StandardInput.WriteLine("exit");
string strRst = pcs.StandardOutput.ReadToEnd();
if (strRst.IndexOf("(0% loss)") != -1)
pingStr = "连接";
else if (strRst.IndexOf("Destination host unreachable.") != -1)
pingStr = "无法到达目的主机";
else if (strRst.IndexOf("Request timed out.") != -1)
pingStr = "超时";
else if (strRst.IndexOf("Unknown host") != -1)
pingStr = "无法解析主机";
else
pingStr = strRst;
pcs.Close();
return pingStr;
}
}
}
posted @ 2009-02-26 15:39 JaggerLee 阅读(441) 评论(0)
编辑
前些天有个朋友拿了一个问题过来,我一看就懵了,Group 组内数据筛选,俺可没玩过
而且貌似SQL 不具备这样的特性,在网上搜集资料之后,用了数据"躲猫猫"的办法变相的实现了功能...
问题如下:
朋友现在正在做并想要的功能,(股价竞猜系统)
每天的收盘价 竟猜 系统,
每天只有一个收盘价 ,但竟猜的数据很多
最后获奖人只有一个 也就是提交数值 跟收盘价在 正负10以内 ,提交最早的一位
写个SQL 自动找出每天 获奖的人
tb_Guesses 这个表是 竟猜表 GuessPrice(客户竟猜价) OpTime(竞猜时间)ProductID(股票ID)
tb_CloQuos 这个是每天的的收盘价表 CloQuotation(每天唯一收盘价) CloTime(收盘时间)ProductID(股票ID)
表结构图示如下:

规则说明:
/////////////////////////////////////
24号如果 有一个 是收盘价 3500
另一个表里24号竟猜价 有 一个 3501 ,3505,3491
我要只取 一个 3501
/////////////////////////////////
如 24号竟猜价 有 3501(添加时间 24号 8点),3501(添加时间 24号 7点), ,3505,3491
那么我只取 3501(添加时间 24号 7点) 这一条数据
以下是我写的 SQL CODE ,刚刚写出来,还不知道怎么优化..有路过的大大知道怎么优化的麻烦帮个忙优化一下

Code
SELECT c.*,Convert(varchar(100),OpTime,23) as theDay,d.theAbs FROM tb_guesses c inner join
(select MIN(a.GuessPrice) as thePrice,MIN(OpTime) as theOpTime,MIN(MinAbs) as theAbs from tb_guesses a,
(select DATEDIFF(d,OpTime,getdate()) as GroupDay,Min(ABS(tg.guessprice - tc.CloQuotation)) as MinAbs,AVG(tc.CloQuotation) as CloQuoPrice from tb_guesses tg inner join tb_CloQuos tc ON tg.ProductID = tc.ProductID AND DATEDIFF(d,tg.OpTime,getdate()) = DATEDIFF(d,tc.CloTime,getdate())
WHERE ABS(tg.guessprice - tc.CloQuotation) <= 10 group by DATEDIFF(d,OpTime,getdate())) b
WHERE DATEDIFF(d,OpTime,getdate()) = b.GroupDay and ABS(a.GuessPrice - b.CloQuoPrice) = b.MinAbs
GROUP BY DATEDIFF(d,OpTime,getdate())) d ON c.GuessPrice = d.thePrice AND c.OpTime = d.theOpTime
posted @ 2009-02-26 09:42 JaggerLee 阅读(675) 评论(1)
编辑