我想做一个ip地址归属地查询的页面,我有那个ip地址段所属地区的txt,麻烦各位大虾给我实现的代码

解决方案 »

  1.   

    只能教你方法,最好别是拿来主义,再说,某位说过受人于鱼不如授人于渔.
    你根据要查找的IP,和你那个txr或者数据库匹配,然后在输出不就行了?
      

  2.   

    关键是我不知道怎么来读取txt文本内容.用stream读取出来的不知道怎么跟实际ip地址对比!
      

  3.   

    look http://lumaqq.linuxsir.org/article/qqwry_format_detail.html
      

  4.   

    哈哈.能不能把那个IP的TXT传给我.我做一下,我给你源码.
      

  5.   

    呵呵.做出来了.不过没有全面测试.未必正确
    vista+vs2008+.net framework3.5IP类代码:using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Data.OleDb;
    using System.Data;namespace IP归属地查询
    {
        class IP
        {
            private string strIP = null;
            public string IPString
            {
                get
                {
                    return strIP;
                }
            }        private IP()
            {        }        public IP(string strIP)
            {
                if (Check(strIP))
                {
                    this.strIP = strIP;
                }
                else
                {
                    throw (new IPException("不正确的IP格式!"));
                }
            }        private bool Check(string strIP)
            {
                if (Regex.IsMatch(strIP, @"^(?!192\.168\.)((([1-9]?[0-9])|1[0-9]{2}|2([0-4][0-9]|5[0-5]))\.){3}(([1-9]?[0-9])|1[0-9]{2}|2([0-4][0-9]|5[0-5]))$"))
                {
                    return true;
                }
                return false;
            }
            public  string GetAddress()
            {
                double dblIP=CacuIP();
                OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\bendibaoip.mdb");
                OleDbDataAdapter ada = new OleDbDataAdapter("SELECT country,city FROM IP_SoftYes WHERE ip1<=" + dblIP + " AND ip2>=" + dblIP, conn);
                DataSet dst=new DataSet();
                ada.Fill(dst);
                if (dst.Tables[0].Rows.Count > 0)
                {
                    return dst.Tables[0].Rows[0][0].ToString() + dst.Tables[0].Rows[0][1].ToString();
                }
                return "对不起!数据库中无此IP信息!";
            }        private double CacuIP()
            {
                double dblIP  = 0;
                string[] arrIPs = this.IPString.Split('.');
                for (int i = 0; i <= 3; i++)
                {                dblIP += Convert.ToDouble(arrIPs[i]) * System.Math.Pow(256, (3 - i));
                }
                return dblIP - 1;
            }
        }
           /// <summary>
        /// IP异常
        /// </summary>
        public class IPException : Exception
        {
            private string strMessage;
            public override string Message
            {
                get
                {
                    return strMessage;
                }
            }
            public IPException(string strMessage)
            {
                this.strMessage = strMessage;
            }    }
       
    }
      

  6.   

    窗体代码:private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    IP ip = new IP(this.textBox1.Text.Trim());
                    this.label1.Text = ip.GetAddress();
                }
                catch (IPException ipe)
                {
                    MessageBox.Show(ipe.Message);            }
            }
      

  7.   

    我这个IP数据库是网上下的mdb文件.格式如下:
    IP1        ip2      country   city67829760   67895295   美国     夏威夷............
    我是根据作者提供的ASP当中的JAVASCRIPTE算法转换过来的.不一定正确.因为对JAVASCRIPT一无所知.
      

  8.   

    哥哥  你这个方法我也会做,但是我想要的是从txt文件中读取并且操作数据,而不是在数据库中读取操作数据