获取最新的IP数据库及如何查询

[img]/upload/attachment/132451/d0cd54e4-8cb7-3a30-8cd8-20902a839198.jpg[/img]

.NET中把IP地址转为长整型的方法:

/// <summary>把IP地址转成长数字,
/// 算法:128.125.1.24 → (128*256*256*256) + (125*256*256) + (1*256) +24
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static ulong IpToLong(string ip)
{
try
{
string[] cip = ip.Trim().Split('.');
string[] aip = new string[4];
cip.CopyTo(aip, 0);
if (cip.Length < 3)
{
for (int i = 3; i > cip.Length; i--)
{
aip[i] = "0";
}
}
uint[] iip = new uint[4];
Regex reg = new Regex(@"\d+");
for (int x = 0; x < aip.Length; x++)
{
if (reg.IsMatch(aip[x]))
iip[x] = Convert.ToUInt32(aip[x]);
else
iip[x] = 0;
}
ulong uip = Convert.ToUInt64(256 * 256 * 256 * iip[0] + 256 * 256 * iip[1] + 256 * iip[2] + iip[3]);
return uip;
}
catch (Exception ess)
{
throw ess;
}
}


我用自己的机器试,一开始就一下更新所有,结果搞到网页超时,后来就通过SQL语句的TOP先把一部分取出来更新好后再更新另一部分,SQL语句如下:

select top 100 * from ipdata where CHARINDEX('.',starip,0)>0

CHARINDEX函数是MSSQL的内置函数,类似于IndexOf。

更新好后询查就可以把要查询的IP转成长整形,然后通过SQL中的BETWEEN..AND..来查询了。

select * from ipdata where @ip between starip and endip


如果不把IP转成长整型的话则查询出来的会有问题的!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值