?? ip2area.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地址字符串轉(zhuǎn)換成等效的數(shù)字值
public static uint IPToInt(string ip)
{
IPAddress IP;
uint ipvalue = 0;
//若為合法的IP地址,則進(jìn)行轉(zhuǎn)換
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;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -