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


/ Published in: C#
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// IP Address (ipv6) Lookup in Bulk Using C# and MySQL Database
  3. /// Free geolocation database can be downloaded at:
  4. /// http://lite.ip2location.com/
  5. /// </summary>
  6.  
  7. void DisplayFileContents(HttpPostedFile file)
  8. {
  9. Stream theStream = FileUpload1.PostedFile.InputStream;
  10. StringBuilder display = new StringBuilder();
  11. using (StreamReader sr = new StreamReader(theStream))
  12. {
  13. string line;
  14. char[] delimiter1 = new char[] { ',' };
  15.  
  16. while ((line = sr.ReadLine()) != null)
  17. {
  18. string[] iplist = line.Split(delimiter1, StringSplitOptions.None);
  19.  
  20. foreach (string IP in iplist)
  21. {
  22. display.Append("IP Address : " + IP.ToString() + "\n");
  23.  
  24. string ip_addr = IP;
  25.  
  26. System.Numerics.BigInteger ip_num = ip_to_number(ip_addr);
  27. String ip_no = ip_num.ToString();
  28. 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";
  29. string[] data = Get_Data(query);
  30.  
  31. string country_code = data[0];
  32. string country_name = data[1];
  33. string city_name = data[2];
  34. string region_name = data[3];
  35. string latitude = data[4];
  36. string longitude = data[5];
  37. string zip_code = data[6];
  38. string time_zone = data[7];
  39.  
  40. //Define the result file that you want to output
  41. string filePath = Server.MapPath("~") + "result.csv";
  42.  
  43. using (StreamWriter Sw = File.AppendText(filePath))
  44. {
  45. string output = string.Format("\"" + IP + "\"" + "," + "\"" + country_code + "\"" + "," + "\"" + country_name + "\"" + "," + "\"" + city_name + "\"" + "," + "\"" + region_name + "\"" + "," + "\"" + latitude + "\"" + "," + "\"" + longitude + "\"" + "," + "\"" + zip_code + "\"" + "," + "\"" + time_zone + "\"");
  46.  
  47. Console.WriteLine(output);
  48. Sw.WriteLine(output);
  49.  
  50. }
  51. Label3.Text = "Result File Path : " + Path.GetFullPath(filePath) + "<br>";
  52. }
  53. }
  54. }
  55.  
  56. Label4.Text = "Please DELETE the result file if you want to upload a new file! <br>";
  57.  
  58. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.