获取最新的IP数据库及如何查询
http://www.agoit.com/upload/attachment/132451/d0cd54e4-8cb7-3a30-8cd8-20902a839198.jpg.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; cip.CopyTo(aip, 0); if (cip.Length < 3) { for (int i = 3; i > cip.Length; i--) { aip = "0"; } } uint[] iip = new uint; Regex reg = new Regex(@"\d+"); for (int x = 0; x < aip.Length; x++) { if (reg.IsMatch(aip)) iip = Convert.ToUInt32(aip); else iip = 0; } ulong uip = Convert.ToUInt64(256 * 256 * 256 * iip + 256 * 256 * iip + 256 * iip + iip); return uip; } catch (Exception ess) { throw ess; } }
我用自己的机器试,一开始就一下更新所有,结果搞到网页超时,后来就通过SQL语句的TOP先把一部分取出来更新好后再更新另一部分,SQL语句如下:
select top 100 * from ipdata whereCHARINDEX('.',starip,0)>0
CHARINDEX函数是MSSQL的内置函数,类似于IndexOf。
更新好后询查就可以把要查询的IP转成长整形,然后通过SQL中的BETWEEN..AND..来查询了。
select * from ipdata where @ip between starip and endip
如果不把IP转成长整型的话则查询出来的会有问题的!!!
页:
[1]