Store IP Address in a Database

AID: 4107
  • Status: Published

1820 points

  • ByNNirmalan
  • TypeTips/Tricks
  • Posted on2010-11-10 at 19:24:37
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.  

So, let's look at a few ways we can store an IP address.

1.  Together as a string in a CHAR(15) column; e.g:
     '192.168.0.1'

2.  Four separate TINYINT columns; e.g.:
     192    168    0    1

3.  Store it as a numeric value (a single INT or LONG column); e.g:
     3232235521

The first method’s advantages are simple programming and it is directly readable, but it requires more storage space.   The second and third methods use comparatively less storage space, but they require additional programming and data handling.

Herewith I am going to share a code snip that used to store IP address as a numeric value

Convert IP address to a Numeric Value
//A.B.C.D <=> A*256^3 + B*256^2 + C*256 + D 
//        <=> A* 16777216 + B*65536 + C*256 + D 
 
long val = (long.Parse(splitArray[0]) * 16777216) + 
                 (long.Parse(splitArray[1]) * 65536) + 
                       (long.Parse(splitArray[2]) * 256) + 
                             (long.Parse(splitArray[3])); 
 
MessageBox.Show(val.ToString());
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window



Convert Numeric Value to IP address
long ipRem1, ipRem2, ipRem3; 
 
// Convert the text value as long 
long convertValue = long.Parse(longValueTextBox.Text.Trim()); 
 
// Re-Calculation 
long ipAdd1 = Math.DivRem(convertValue, 16777216, out ipRem1); 
long ipAdd2 = Math.DivRem(ipRem1, 65536, out ipRem2); 
long ipAdd3 = Math.DivRem(ipRem2, 256, out ipRem3); 
 
// Construct IP address 
string ipAddress = ipAdd1.ToString().Trim() + "."  
                     + ipAdd2.ToString().Trim() + "."  
                         + ipAdd3.ToString().Trim() + "."  
                           + ipRem3.ToString().Trim(); 
 
MessageBox.Show(convertValue + " ---> " + ipAddress); 
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:

Select allOpen in new window



Also you can check the validity of the single-value IP address by using this function:
/// <summary> 
/// Checks the validity. 
/// </summary> 
/// <param name="values">The values.</param> 
/// <returns></returns> 
private static bool CheckValidity(IEnumerable<string> values) 
{ 
    foreach(var val in values) 
    { 
        int tempval; 
 
        try 
        { 
            tempval = Int32.Parse(val); 
        } 
        catch(Exception) 
        { 
            return false; 
        } 
 
        if (tempval < 0 || tempval > 255) 
        { 
            return false; 
        } 
    } 
    return true; 
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:

Select allOpen in new window

Asked On
2010-11-10 at 19:24:37ID4107
Tags

IP address

,

Database

,

Store IP address

Topic

Microsoft Visual C#.Net

Views
1271

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Visual C# Experts

  1. kaufmed

    39,306

    0 points yesterday

    Profile
    Rank: Genius
  2. JamesBurger

    29,150

    0 points yesterday

    Profile
    Rank: Sage
  3. CodeCruiser

    16,200

    0 points yesterday

    Profile
    Rank: Genius
  4. AndyAinscow

    13,836

    0 points yesterday

    Profile
    Rank: Genius
  5. nishantcomp2512

    13,260

    0 points yesterday

    Profile
    Rank: Wizard
  6. emoreau

    8,800

    0 points yesterday

    Profile
    Rank: Genius
  7. jonnidip

    7,868

    0 points yesterday

    Profile
    Rank: Master
  8. Idle_Mind

    7,162

    0 points yesterday

    Profile
    Rank: Savant
  9. mas_oz2003

    6,750

    0 points yesterday

    Profile
    Rank: Genius
  10. Sudhakar-Pulivarthi

    6,532

    0 points yesterday

    Profile
    Rank: Guru
  11. Michael74

    6,018

    0 points yesterday

    Profile
    Rank: Wizard
  12. RolandDeschain

    6,000

    0 points yesterday

    Profile
    Rank: Sage
  13. ddayx10

    5,300

    0 points yesterday

    Profile
    Rank: Sage
  14. apeter

    5,232

    0 points yesterday

    Profile
    Rank: Sage
  15. TheLearnedOne

    5,000

    0 points yesterday

    Profile
    Rank: Savant
  16. navneethegde

    5,000

    0 points yesterday

    Profile
    Rank: Wizard
  17. mroonal

    4,900

    0 points yesterday

    Profile
    Rank: Sage
  18. naman_goel

    4,664

    0 points yesterday

    Profile
    Rank: Guru
  19. AhmedHindy

    4,500

    0 points yesterday

    Profile
  20. mlmcc

    4,000

    0 points yesterday

    Profile
    Rank: Savant
  21. UndefinedException

    4,000

    0 points yesterday

    Profile
  22. ged325

    3,468

    0 points yesterday

    Profile
    Rank: Genius
  23. keyu

    3,400

    0 points yesterday

    Profile
    Rank: Master
  24. nilhan

    3,200

    0 points yesterday

    Profile
  25. SStory

    3,136

    0 points yesterday

    Profile
    Rank: Sage

Hall Of Fame