Web Hosting Info

Search:

featured partner

The IP to Country Database

  Forum Topics : Support / SQL Server 2000 Complete ASP.NET Solution w/VB.NET Code Behind
Submitted by Holtmelder on Thu, 09/18/2003 - 17:02.
Procedure

1) Edit IPCountry.csv (with excel)
- Delete the Column with the 3 letter country abbreviations unless you need it! I didn't need it

2) Edit IPCountry.csv (with excel)
- In the very first row do an insert and put:
ipFROM
ipTO
countrySHORT
countryLONG
(Doing this makes it easier to import into SQL Server 2000)

3) Import the IPCountry.csv with the new modifications into your SQL Server Database into a table called "tblIPCountry"

4) Once you imported it you should have a design view of:
[tblIPCountry]
ipFROM (example 1003230032)
ipTO (example 1123892933)
countrySHORT (example US)
countryLONG (example United States)

5) Use the following SQL Query from ASP.NET or VB.NET or whatever -
SELECT countrySHORT, countryLONG
FROM tblIPCountry
WHERE '" & ConvertToLong(IPAddress) & "'
BETWEEN ipFROM AND ipTO

6) Here is the function that will convert an IP Address from it's IP format to Long.
------------------------------------------------------------
Private Function ConvertToLong(ByVal IPAddress As Object) As Object

Dim x As Integer
Dim Pos As Integer
Dim PrevPos As Integer
Dim Num As Integer

If UBound(Split(IPAddress, ".")) = 3 Then
' On Error Resume Next
For x = 1 To 4
Pos = InStr(PrevPos + 1, IPAddress, ".", 1)
If x = 4 Then Pos = Len(IPAddress) + 1
Num = Int(Mid(IPAddress, PrevPos + 1, Pos - PrevPos - 1))
If Num > 255 Then
ConvertToLong = "0"
Exit Function
End If
PrevPos = Pos
ConvertToLong = ((Num Mod 256) * (256 ^ (4 - x))) + ConvertToLong
Next
End If

End Function
-------------------------------------------------------------


Summary:

You have the tools now to convert a users IP Address to Long first. Then submit that value to the database and it will return the Country the IP is from.

Enjoy

HoltMelder
Comment viewing options:
Select your preferred way to display the comments and click 'Save settings' to submit your changes.
IPAddress.Parse
Posted by Tri on Wed, 10/08/2003 - 01:49.
Or you could just use System.Net.IPAddress.Parse().

-Tri
Code snippet of using IPAddress.Parse
Posted by nate on Mon, 07/31/2006 - 06:37.
Actually implementation of Tri's suggestion:
Dim IPBytes As Byte() = IPAddress.Parse().GetAddressBytes

Dim IPLong As String = ""
For i As Integer = 0 To IPBytes.Length - 1
    IPLong &= IPBytes(i)
Next

IPLong will have the value you need.

-Nathan.


--
3Bit Solutions
Your challenge. Our solution.