Click here to Skip to main content
6,306,412 members and growing! (16,633 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Exracting countries and cities' name from IP address

By John.Jiang

This sample decrbes how to get name from IP
C# 2.0, Windows, .NET 2.0VS2005, Dev
Posted:9 Sep 2007
Views:6,336
Bookmarked:10 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
6 votes for this article.
Popularity: 1.19 Rating: 1.53 out of 5
5 votes, 83.3%
1

2
1 vote, 16.7%
3

4

5

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 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,datalayer 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!");
            }
        }
    }
}
 
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;
        }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

John.Jiang


Member
Treaple offshore outsourcing software services providing mobile(Pocket pc,smartphone and wince.net) software,mobile GIS(Mobile map),Desktop GIS(Desktop map), GPS,GSM Locating Services,Voip,Multimedia(Audio,Video) and web design offshore outsourcing software development services.We have developed lots of projects on Microsoft Poccket pc 5.0/6.0,smartphone 5.0/6.0 and Microsoft windows and got strong background in Microsoft MapPoint, ESRI ArcGIS, Map info,Google map etc
Our website below: http://www.treaple.com
Occupation: Software Developer (Senior)
Company: www.treaple.com
Location: China China

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralThank you! PinmemberJohn.Jiang8:28 10 Sep '07  
GeneralSource code PinmemberJohn.Jiang17:07 9 Sep '07  
GeneralRe: Source code PinsupporterDrew Stainton17:15 9 Sep '07  
GeneralRe: Source code [modified] PinmemberJohn.Jiang17:29 9 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Sep 2007
Editor:
Copyright 2007 by John.Jiang
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project