
.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轉成長整型的話則查詢出來的會有問題的!!!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
