我想做一个ip地址归属地查询的页面,我有那个ip地址段所属地区的txt,麻烦各位大虾给我实现的代码
解决方案 »
- Coolite.Ext.Web.MenuItem 动态添加 item
- 小弟请个位前辈、高手指教,有"GirdView"为什么还要用"DataGrid"?
- devTreeList绑定多张表
- .net websercie,客户端,怎么获取到发出的xml和收到的xml?
- 求:根据DataGrid列值的不同改变该行的背景色的代码
- 我想让服务器的exe程序每隔一段时间去扫描一次数据表,但不要影响服务器性能?
- 图片保存和打印问题
- 如何用C#在基于WEB的方式下控制扫仪描
- 有一个函数System.Data.Datatable myfunction()传值问题
- 用c#编写程——输出1900-2019之间的所有闰年(每行五个
- 关于Timer控件 谢谢
- Font构造函数问题???
你根据要查找的IP,和你那个txr或者数据库匹配,然后在输出不就行了?
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;
} }
}
{
try
{
IP ip = new IP(this.textBox1.Text.Trim());
this.label1.Text = ip.GetAddress();
}
catch (IPException ipe)
{
MessageBox.Show(ipe.Message); }
}
IP1 ip2 country city67829760 67895295 美国 夏威夷............
我是根据作者提供的ASP当中的JAVASCRIPTE算法转换过来的.不一定正确.因为对JAVASCRIPT一无所知.