Store IP Address in Data Base best way

Solution 1:

IP v4 addresses are 4 bytes, IP v6 addresses are 16 bytes, so store them as a varbinary(16) field. I see from your question tags that you are using .Net so you will be able to get these bytes easily using IPAddress.GetAddressBytes(). The following code will be useful to you if you're using Entity Framework or a similar ORM.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;

    [Required, MinLength(4), MaxLength(16)]
    public byte[] IPAddressBytes { get; set; }

    [NotMapped]
    public IPAddress IPAddress
    {
        get { return new IPAddress(IPAddressBytes); }
        set { IPAddressBytes = value.GetAddressBytes(); }
    }

Solution 2:

"Octects in four tinyint columns." http://web.archive.org/web/20150511204915/http://sqlserver2000.databases.aspfaq.com/how-should-i-store-an-ip-address-in-sql-server.html

Solution 3:

Simplest thing is to store a char(15), assuming IPV4.

This is the simplest form to use, does not required complex conversions and calculations and unless you are storing very large amounts of data (millions of records), performance should not factor in.