Return to Snippet

Revision: 68632
at January 30, 2015 18:56 by Hexahow


Initial Code
/// <summary>
/// IP Address (ipv6) Lookup in Bulk Using C# and MySQL Database
/// Free geolocation database can be downloaded at:
/// http://lite.ip2location.com/
/// </summary>

void DisplayFileContents(HttpPostedFile file)
   {
       Stream theStream = FileUpload1.PostedFile.InputStream;
       StringBuilder display = new StringBuilder();
       using (StreamReader sr = new StreamReader(theStream))
       {
           string line;
           char[] delimiter1 = new char[] { ',' };

           while ((line = sr.ReadLine()) != null)
           {
               string[] iplist = line.Split(delimiter1, StringSplitOptions.None);

               foreach (string IP in iplist)
               {
                   display.Append("IP Address : " + IP.ToString() + "\n");

                   string ip_addr = IP;

                   System.Numerics.BigInteger ip_num = ip_to_number(ip_addr);
                   String ip_no = ip_num.ToString();
                   string query = "SELECT country_code, country_name , city_name , region_name, latitude,longitude,zip_code,time_zone FROM ip2location_db11 WHERE ip_to >= " + ip_no + " order by ip_to limit 1";
                   string[] data = Get_Data(query);

                   string country_code = data[0];
                   string country_name = data[1];
                   string city_name = data[2];
                   string region_name = data[3];
                   string latitude = data[4];
                   string longitude = data[5];
                   string zip_code = data[6];
                   string time_zone = data[7];

                   //Define the result file that you want to output
                   string filePath = Server.MapPath("~") + "result.csv";

                   using (StreamWriter Sw = File.AppendText(filePath))
                   {
                       string output = string.Format("\"" + IP + "\"" + "," + "\"" + country_code + "\"" + "," + "\"" + country_name + "\"" + "," + "\"" + city_name + "\"" + "," + "\"" + region_name + "\"" + "," + "\"" + latitude + "\"" + "," + "\"" + longitude + "\"" + "," + "\"" + zip_code + "\"" + "," + "\"" + time_zone + "\"");

                       Console.WriteLine(output);
                       Sw.WriteLine(output);

                   }
                   Label3.Text = "Result File Path : " + Path.GetFullPath(filePath) + "<br>";
               }
           }
       }

       Label4.Text = "Please DELETE the result file if you want to upload a new file! <br>";

   }

Initial URL
http://lite.ip2location.com/

Initial Description
Use the code below to lookup IP address in bulk using C-Sharp programming languages and IP2Location MySQL database. In this tutorial, we use the IP2Location LITE database to lookup country of origin from the visitor's IP address. Free databases are available for download at IP2Location LITE database.

Initial Title
IP Address (ipv6) Lookup in Bulk Using C# and MySQL Database

Initial Tags
ip

Initial Language
C#