Introduction:
Sometimes we want to get visitors details that visit our web site or using client connecting our server. This sample describes how to translate IP address and query country and city from IP address.
Background:
I pent whole day to translate IP database to English, but I guess some field is going not very well.
So I hope somebody can help to improve the IP database.
Using the code:
Here are three parts code unit which includes logic layer, data layer and control layer. And if you want to run the sample in your local host, you must copy IP.mdb to drive c:\
Part 1.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Treaple.com
{
public partial class Form1 : Form
{
private CompareIP compareIP = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
compareIP = new CompareIP();
string Ip = txtIP.Text.Trim();
string[]Ip_List = Ip.Split(".".ToCharArray());
string X_Ip = "";
foreach (string ip in Ip_List)
{
if (Convert.ToInt16(ip) < 16)
X_Ip += "0" + Convert.ToInt16(ip).ToString("x");
else
X_Ip += Convert.ToInt16(ip).ToString("x");
}
long N_Ip = long.Parse(X_Ip,
System.Globalization.NumberStyles.HexNumber);
compareIP.IPAddress = N_Ip;
DataSet newdata = compareIP.GetIP();
try
{
txtCountry.Text = newdata.Tables[0].Rows[0][2].ToString();
txtCity.Text = newdata.Tables[0].Rows[0][3].ToString();
}
catch
{
MessageBox.Show("Invalid Ip address!");
}
}
}
}
Part 2:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace Treaple.com
{
//////////////////////Base Class///////////////////////
public abstract class DataExchange
{
public IPAccessDataBase accessDataBase;
public abstract int AddData();
public abstract int ChangeData();
public abstract DataSet GetData();
}
Join the conversation! Your thoughts help the community grow.