使用SQLDataReader读取IP地址

时间:2016-11-17 16:48:44

标签: c# sql asp.net .net vb.net

使用ASP.Net应用程序,我想使用SQLDataReader从SQL数据库中提取IPv4信息。从SQL读取IP时,数据类型是二进制的,我不确定处理此问题的最佳方法。

由于SQLDataReader.GetIPAddress()没有方法,我似乎需要使用SQLDataReader.GetBytes()?关于如何执行此操作的许多示例在线,因为getstring非常简单我希望有另一种更简单的方法来使用getbyte()或其他方法来引入IP。

    Do While SQLReader.Read()
        Dim lastcom As System.DateTime = SQLReader.GetDateTime(0)
        Debug.WriteLine(lastcom.ToString("MM/dd/yyyy HH:mm:ss.fff")) 
        Debug.WriteLine(SQLReader.GetString(1)) 
        Debug.WriteLine(SQLReader.GetBoolean(2)) 
        Debug.WriteLine(SQLReader.GetString(3)) 
        Debug.WriteLine(SQLReader.GetString(4)) 
        Label_IP = SQLReader.GetString(1)            
        Label_Model.Text = SQLReader.GetString(4)
   Loop

1 个答案:

答案 0 :(得分:0)

我正在假设IP地址如何存储在数据库中(并且只支持IPv4地址),但您可以尝试这样的事情:

Dim ipAddr(3) As Byte
' Assuming that column #1 is your IP address column
Dim byteCount = SQLReader.GetBytes(1, 0, ipAddr, 0, 4)
If byteCount = 4 Then
    Label_IP.Text = New IPAddress(ipAddr).ToString()
Else
    Console.WriteLine("Expected 4 bytes for IP address")
End If
相关问题