Extracting the Country from IP Address

CK CK 3788 
Created at  
465 0 0



Notes: If you want to run this sample,you should copy the Ip address to driver c:.


Using the code


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
{
    ///
    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;
        }
    }  
}

[caption id="" align="aligncenter" caption="Download Source Code"]1118008935.zip[/caption]
Tags C# C# Graphics Facebook X
Comments 0
Similar posts
  1. Graphics in C# - Irregular Forms
    905
  2. How to restrict a program to a single instance
    727
  3. The best way to enable and disable buttons, menu items and toolbar buttons
    666
  4. How to speed up access to the RichTextBox Lines property
    828
  5. How to print character in C# by ASCII code
    475
  6. How To Print Text In C#
    453
  7. How to send binary data through C# CGI app?
    993
  8. Calling Your Main Thread From A Worker Thread In C#
    544
  9. How To Read And Write A Cookie
    359
  10. Using C# With Cookies
    620
  11. To load image from file
    446
  12. How to convert bitmap image to jpeg image ?
    353
  13. How to resize and save PNG or Jpeg Image file ?
    629
  14. How to convert text string to integer ?
    608
  15. How to convert integer to text string ?
    696
  16. How to get process id in C# like windows task manager ?
    867
  17. How To Check If Your Printer Is Connected Using C#
    1,025
  18. How to download/grap web pages or XML document at internet website in Delphi XE?
    868
  1. How to get GM Time as String ?
    422
  2. Makefile to automate the build process for C++
    276
  3. Finding process name on OS
    405
  4. Convert System.DateTime to UNIX timestamp
    380
  5. Retrieve system information
    494
  6. How to call SetTimer function on MFC CDialog class ?
    650
  7. How to rename(change file name) in C# ?
    1,072
  8. Find Files in certain directory
    924
  9. Implementing C#'s foreach loop in Delphi 8
    459
  10. How to detect resource leaks in a Form
    936
  11. The best way to enable and disable buttons, menu items and toolbar buttons
    666
  12. How to exit from a console application
    809
  13. How to restrict a program to a single instance
    727
  14. Compress and decompress strings in C#
    970
  15. Graphics in C# - Irregular Forms
    905
  16. How to run shell command by MFC ?
    434
  17. Binary Search Sample Code
    403
  18. Double Linked List based in C++
    796
  19. String Replace Function For C#
    1,542
  20. How to print character in C# by ASCII code
    475
  21. MFC based World Wide Web HTTP Server Source Code
    458
  22. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    470
  23. UDP Send and Receive Using CAsyncSocket
    1,204
  24. Using the shell to receive notification of removable media being inserted or removed
    680
  25. Adding Custom Paper Sizes To Named Printers
    644
  26. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    971
  27. A Customizable Printing Text Class
    427
  28. Simplified .NET Printing In C#
    689
  29. How To Print Text In C#
    453
  30. C# Lists
    1,146
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,711
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    198
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    232
  17. The Concept of Scaffolding in AI Agents
    221
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    576
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    253
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    327
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252