Community discussion forum

How to get coutries and cities' name from IP

  • 2 years ago

    Introduction

    Sometimes we want to get visitors details who visit our web site or using client connecting our server.This sample descirbes how to translate IP address and query countrie and city from IP address

    Background

    I pent whole day to translate IP database to English,but I guess some 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,datalayer and control layer.and if you want to run the sample in your local host ,You must copy IP.mdb to drive c:\

    Collapse
    //
     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!");
                }
            }
        }
    }
     
    Part2.
     
    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();
        }
    
        //////////////////////Sub Class////////////////////////
    
        public class CompareIP
        {
            public IPAccessDataBase iPAccessDataBase;
            public long IPAddress;
    
            public CompareIP()
            {
                iPAccessDataBase = new IPAccessDataBase();
            }
    
            public DataSet GetIP()
            {
                return iPAccessDataBase.SelectData("select * from address where " + this.IPAddress + " >=ip1 and " + this.IPAddress + " <= ip2", "address");
            }
        }
    }
     
    part3. 
     
    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    
    namespace Treaple.com
    {
        /// </summary>
        public class IPAccessDataBase
        {
            private string strSQL;
    
            private string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +"c:\\"+("IP.MDB"); 
    
            private OleDbConnection myConnection;
            private OleDbCommandBuilder sqlCmdBld;
            private OleDbDataAdapter da;
    
            private DataSet ds = new DataSet();
    
            public IPAccessDataBase()
            {
            }
    
            public IPAccessDataBase(string conStr)
            {
                connectionString = conStr;
            }
    
            public DataSet SelectData(string tempStrSQL, string tempTableName)
            {
                this.strSQL = tempStrSQL;
                this.myConnection = new OleDbConnection(connectionString);
                this.da = new OleDbDataAdapter(this.strSQL, this.myConnection);
                this.ds.Clear();
                this.da.Fill(ds, tempTableName);
                return ds;
            }
    
            public DataSet UpdateData(DataSet changedDataSet, string tableName)
            {
                this.myConnection = new OleDbConnection(connectionString);
                this.da = new OleDbDataAdapter(this.strSQL, this.myConnection);
                this.sqlCmdBld = new OleDbCommandBuilder(da);
                this.da.Update(changedDataSet, tableName);
                return changedDataSet;
            }
    
     
    
            public DataTable SelectData(string tempStrSQL)
            {
                this.myConnection = new OleDbConnection(connectionString);
                DataSet tempDataSet = new DataSet();
                this.da = new OleDbDataAdapter(tempStrSQL, this.myConnection);
                this.da.Fill(tempDataSet);
                return tempDataSet.Tables[0];
            }
    
    
            public int UpdateData(string tempStrSQL)
            {
                OleDbConnection myConnection = new OleDbConnection(connectionString);
                OleDbCommand myCommand = new OleDbCommand(tempStrSQL);
                myCommand.Connection = myConnection;
                myConnection.Open();
                int intNumber = myCommand.ExecuteNonQuery();
                myCommand.Connection.Close();
                myConnection.Close();
                return intNumber;
            }
        }
    
       
    }
    
    

    Source code

    The source is too large to put here.So if you are going to get the source code,please visit http://www.treaple.com/ArticlesDetails.aspx?id=64  to download it.

  • 2 years ago
  • 2 years ago

    Hi,

       I just checked out the link.This article's subject is similar to that one.But I think something is defferent from that one.I put my own IP database and defferent way to carry out the sample.Thanks.

    Cheers. 

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback