在数据库中存储IP地址

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.  

IP地址可以通过多种方式存储在数据库中。 这些方式可能会根据数据量而有所不同。 我正在处理大量数据以进行用户身份验证,因此需要一种最小化存储的方法。

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

因此,让我们看一下存储IP地址的几种方法。

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

1.作为字符串一起在CHAR(15)列中使用; 例如:

     '192.168.0.1'

'192.168.0.1'

2.  Four separate TINYINT columns; e.g.:

2.四个独立的TINYINT列; 例如:

     192    168    0    1

192168 0 1

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

3.将其存储为数值(单个INT或LONG列); 例如:

     3232235521

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

我将与您分享一个代码片段,该片段用于将IP地址存储为数字值

Convert IP address to a Numeric Value

将IP地址转换为数值

//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());

Convert Numeric Value to IP address

将数值转换为IP地址

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); 

Also you can check the validity of the single-value IP address by using this function:

您也可以使用以下功能检查单值IP地址的有效性:

/// <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; 
}

翻译自: https://www.experts-exchange.com/articles/4107/Store-IP-Address-in-a-Database.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值