这是 Sql Server 函数
CREATE FUNCTION dbo.f_IP2Int(
@ip char(15)
)RETURNS bigint
AS
BEGIN
DECLARE @re bigint
SET @re=0
SELECT @re=@re+LEFT(@ip,CHARINDEX('.',@ip+'.')-1)*ID
,@ip=STUFF(@ip,1,CHARINDEX('.',@ip+'.'),'')
FROM(
SELECT ID=CAST(16777216 as bigint)
UNION ALL SELECT 65536
UNION ALL SELECT 256
UNION ALL SELECT 1)a
RETURN(@re)
END
帮忙转换为 C# 代码 谢谢!!!
public int f_IP2Int(string ip)
{
int re=0;
string sql="SELECT @re=@re+LEFT(@ip,CHARINDEX('.',@ip+'.')-1)*ID
,@ip=STUFF(@ip,1,CHARINDEX('.',@ip+'.'),'')
FROM(
SELECT ID=CAST(16777216 as bigint)
UNION ALL SELECT 65536
UNION ALL SELECT 256
UNION ALL SELECT 1)a
";
SqlConnection conn=new SqlConnection(连接字符串);
conn.Open();
SqlCommand cmd=new SqlCommand(conn);
SqlDataReader dr=cmd.ExcuteReader();
......
}
{
int re=0;
string sql="SELECT @re=@re+LEFT(@ip,CHARINDEX('.',@ip+'.')-1)*ID
,@ip=STUFF(@ip,1,CHARINDEX('.',@ip+'.'),'')
FROM(
SELECT ID=CAST(16777216 as bigint)
UNION ALL SELECT 65536
UNION ALL SELECT 256
UNION ALL SELECT 1)a
";
SqlConnection conn=new SqlConnection(连接字符串);
conn.Open();
SqlCommand cmd=new SqlCommand(conn);
SqlDataReader dr=cmd.ExcuteReader();
......
}
温馨提示:答案为网友推荐,仅供参考