⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ip2area.cs

📁 IP地址转地区
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;

namespace IP2Area
{
    public class IP2Area
    {
        public static string GetArea(string ip)
        {
            string area = "";
            try
            {
                string conn = ConfigurationManager.ConnectionStrings["Conn_IP"].ConnectionString;
                SqlConnection connection = new SqlConnection(conn);
                connection.Open();
                string sql = "select top 1 area from tb_ip where startip_int<" + IPToInt(ip) + " and endip_int>" + IPToInt(ip) + " order by startip_int desc,endip_int asc";
                SqlCommand cmd = new SqlCommand(sql, connection);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                    area = dr["area"].ToString();
                }
                connection.Close();
            }
            catch (Exception e)
            {
            }
            return area;
        }

        public static string GetCity(string ip)
        {
            string city = "";
            try
            {
                string conn = ConfigurationManager.ConnectionStrings["Conn_IP"].ConnectionString;
                SqlConnection connection = new SqlConnection(conn);
                connection.Open();
                string sql = "select top 1 city from tb_ip where startip_int<" + IPToInt(ip) + " and endip_int>" + IPToInt(ip) + " order by startip_int desc,endip_int asc";
                SqlCommand cmd = new SqlCommand(sql, connection);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                    city = dr["city"].ToString();
                }
                connection.Close();
            }
            catch (Exception e)
            {
            }
            return city;
        }

        public static string GetProvince(string ip)
        {
            string province = "";
            try
            {
                string conn = ConfigurationManager.ConnectionStrings["Conn_IP"].ConnectionString;
                SqlConnection connection = new SqlConnection(conn);
                connection.Open();
                string sql = "select top 1 province from tb_ip where startip_int<" + IPToInt(ip) + " and endip_int>" + IPToInt(ip) + " order by startip_int desc,endip_int asc";
                SqlCommand cmd = new SqlCommand(sql, connection);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                    province = dr["province"].ToString();
                }
                connection.Close();
            }
            catch (Exception e)
            {
            }
            return province;
        }

        //将IP地址字符串转换成等效的数字值
        public static uint IPToInt(string ip)
        {
            IPAddress IP;
            uint ipvalue = 0;
            //若为合法的IP地址,则进行转换
            if (IPAddress.TryParse(ip, out IP))
            {
                String[] _ip = IP.ToString().Split('.');
                for (int i = 0; i < 4; i++)
                {
                    if (i != 3)
                    {
                        ipvalue = ipvalue + (uint)System.Convert.ToUInt32(_ip[i]) << 8;
                    }
                }
                ipvalue += (uint)System.Convert.ToUInt32(_ip[3]);
            }
            return ipvalue;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -