This question already has an answer here:
What should be the most recommended datatype for storing an IPv4 address in SQL server?
Or maybe someone has already created a user SQL data-type (.Net assembly) for it?
I don't need sorting.
This question already has an answer here:
What should be the most recommended datatype for storing an IPv4 address in SQL server?
Or maybe someone has already created a user SQL data-type (.Net assembly) for it?
I don't need sorting.
Quoting this:
I normally just use varchar(15) for IPv4 addresses - but sorting them is a pain unless you pad zeros.
I've also stored them as an INT in the past.
System.Net.IPAddress
has aGetAddressBytes
method that will return the IP address as an array of the 4 bytes that represent the IP address. You can use the following C# code to convert anIPAddress
to anint
...I had used that because I had to do a lot of searching for dupe addresses, and wanted the indexes to be as small & quick as possible. Then to pull the address back out of the int and into an
IPAddress
object in .net, use theGetBytes
method onBitConverter
to get the int as a byte array. Pass that byte array to the constructor forIPAddress
that takes a byte array, and you end back up with theIPAddress
that you started with.I'd probably go with a
varchar
orchar
.And set the size to 15.