Switch to full style
C# examples
Post a reply

IP Address Lookup in Bulk Using C-Sharp and MySQL Database

Fri Jan 30, 2015 9:44 am

Hi, you may lookup IP address in bulk using C-Sharp programming languages and IP2Location MySQL database. In this snippet, 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.


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>";

   }




Post a reply
  Related Posts  to : IP Address Lookup in Bulk Using C-Sharp and MySQL Database
 dump Mysql database     -  
 import Mysql database     -  
 Creating a Database in MySQL     -  
 Optimize All Tables In A MySQL Database     -  
 what is MAC address     -  
 Find MAC Address     -  
 Get Server address     -  
 change the MAC address     -  
 Get IP address code     -  
 log visitor IP address.     -