Home > Blockchain >  Insert existing database file in project using C#
Insert existing database file in project using C#

Time:07-02

I am working on an application, which gets the public IP address of the user, looks up in a database for the location of that IP address, gets the Latitude an Longitude and finally displays the time of sunrise and sunset at that place.

To make step two work, I need to implement a database file in my project. I have downloaded the database file (.bin - format) already but I couldn't make it work to connect my program with the downloaded database file.

Download of database file below:

enter image description here

How can I solve this?

Cheers!

CodePudding user response:

You can use the IP2Location NuGet package https://www.nuget.org/packages/IP2Location.IPGeolocation/ and call it like below:

Dim oIPResult As New IP2Location.IPResult
Dim oIP2Location As New IP2Location.Component
Try
    Dim strIPAddress = "8.8.8.8"
    If strIPAddress.Trim <> "" Then
        oIP2Location.Open("C:\myfolder\IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY.BIN", True)
        oIPResult = oIP2Location.IPQuery(strIPAddress)
        Select Case oIPResult.Status
            Case "OK"
                Console.WriteLine("IP Address: " & oIPResult.IPAddress)
                Console.WriteLine("City: " & oIPResult.City)
                Console.WriteLine("Country Code: " & oIPResult.CountryShort)
                Console.WriteLine("Country Name: " & oIPResult.CountryLong)
                Console.WriteLine("Postal Code: " & oIPResult.ZipCode)
                Console.WriteLine("Domain Name: " & oIPResult.DomainName)
                Console.WriteLine("ISP Name: " & oIPResult.InternetServiceProvider)
                Console.WriteLine("Latitude: " & oIPResult.Latitude)
                Console.WriteLine("Longitude: " & oIPResult.Longitude)
                Console.WriteLine("Region: " & oIPResult.Region)
                Console.WriteLine("TimeZone: " & oIPResult.TimeZone)
                Console.WriteLine("NetSpeed: " & oIPResult.NetSpeed)
                Console.WriteLine("IDD Code: " & oIPResult.IDDCode)
                Console.WriteLine("Area Code: " & oIPResult.AreaCode)
                Console.WriteLine("Weather Station Code: " & oIPResult.WeatherStationCode)
                Console.WriteLine("Weather Station Name: " & oIPResult.WeatherStationName)
                Console.WriteLine("MCC: " & oIPResult.MCC)
                Console.WriteLine("MNC: " & oIPResult.MNC)
                Console.WriteLine("Mobile Brand: " & oIPResult.MobileBrand)
                Console.WriteLine("Elevation: " & oIPResult.Elevation)
                Console.WriteLine("Usage Type: " & oIPResult.UsageType)
                Console.WriteLine("Address Type: " & oIPResult.AddressType)
                Console.WriteLine("Category: " & oIPResult.Category)
            Case "EMPTY_IP_ADDRESS"
                Console.WriteLine("IP Address cannot be blank.")
            Case "INVALID_IP_ADDRESS"
                Console.WriteLine("Invalid IP Address.")
            Case "MISSING_FILE"
                Console.WriteLine("Invalid Database Path.")
        End Select
    Else
        Console.WriteLine("IP Address cannot be blank.")
    End If
Catch ex As Exception
    Console.WriteLine(ex.Message)
Finally
    oIP2Location.Close()
    oIPResult = Nothing
    oIP2Location = Nothing
End Try

Example code came from https://github.com/ip2location/ip2location-dotnet.

  • Related