判断ip 所在城市
发布时间:2020-12-12 14:36:12 所属栏目:MsSql教程 来源:网络整理
导读:可先下载ip 数据库 sqlserver 2005 版 ?点击下载 public static string GetIpRealWorldAddress(string ipAddress) ? ? ? ? { ? ? ? ? ? ? if (!IpAddressAvailable(ipAddress)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return "ip地址有问题"; ? ? ? ? ? ? } ? ? ? ?
可先下载ip 数据库 sqlserver 2005 版 ?点击下载 public static string GetIpRealWorldAddress(string ipAddress) ? ? ? ? { ? ? ? ? ? ? if (!IpAddressAvailable(ipAddress)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return "ip地址有问题"; ? ? ? ? ? ? } ? ? ? ? ? ? long value = GetIPCount(ipAddress); ? ? ? ? ? ? string Sql = string.Format("select * from ipLib where convert(float,ip_1)<= {0} and convert(float,ip_2) >= {0}",value); ? ? ? ? ? ? using (SqlConnection _SqlConnection = new SqlConnection("server=127.0.0.1;database=fy;uid=sa;pwd=**********")) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? SqlCommand _SqlCommand = new SqlCommand(Sql,_SqlConnection); ? ? ? ? ? ? ? ? _SqlConnection.Open(); ? ? ? ? ? ? ? ? SqlDataReader _SqlDataReader = _SqlCommand.ExecuteReader(); ? ? ? ? ? ? ? ? if (_SqlDataReader.Read()) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? string country = (string)_SqlDataReader["address"]; ? ? ? ? ? ? ? ? ? ? return country; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? return "没有找到匹配的记录!"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //取得ip的long值 3.254.255.255 = 3*256^3 + 254 *256^2? ? ? ? ? public static long GetIPCount(string ipAddress) ? ? ? ? { ? ? ? ? ? ? ipAddress = ipAddress.Trim(); ? ? ? ? ? ? string[] ipSecs = ipAddress.Split('.'); ? ? ? ? ? ? long value = 0; ? ? ? ? ? ? for (int i = 0; i < 4; i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? int ipSecDec = int.Parse(ipSecs[i]); ? ? ? ? ? ? ? ? int power = 3 - i; ? ? ? ? ? ? ? ? long ipSecValue = (long)(ipSecDec * Math.Pow(256,power)); ? ? ? ? ? ? ? ? value = value + ipSecValue; ? ? ? ? ? ? } ? ? ? ? ? ? value = value + 1; ? ? ? ? ? ? return value; ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 判断ip地址是否有问题 ?1 地址段数, 地址段数里面是否是数字,数字是否在 0-255范围内 ? ? ? ? /// 从以上三个方面监测 ? ? ? ? /// </summary> ? ? ? ? /// <param name="ipAddress"></param> ? ? ? ? /// <returns></returns> ? ? ? ? private static bool IpAddressAvailable(string ipAddress) ? ? ? ? { ? ? ? ? ? ? ipAddress = ipAddress.Trim(); ? ? ? ? ? ? string[] ipSecs = ipAddress.Split('.'); ? ? ? ? ? ? if (ipSecs.Length != 4) return false; ? ? ? ? ? ? //如果每个段都可以转为int则返回真 ? ? ? ? ? ? for (int i = 0; i < ipSecs.Length; i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? try ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? int ipSecDec = int.Parse(ipSecs[i]); ? ? ? ? ? ? ? ? ? ? if (ipSecDec < 0 || ipSecDec > 255) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? catch ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? return true; ? ? ? ? }(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- SQLSERVER 执行sp_who2 显示的LastBatch列时间显示不正确
- SQL Server规范化策略:varchar vs int Identity
- Mysql复制表结构、表数据的方法
- SQL2005企业管理器远程连接非默认端口SQL2005数据库的解决办
- MySQL数据库备份方法说明
- 解决SQLSERVER在还原数据时出现的“FILESTREAM功能被禁用”
- 在SQL中有count(*)和count()之间有区别
- sql-server – 为什么T-SQL ISNULL()截断字符串和COALESCE不
- sql-server-2008 – 使用带有快速加载目标的表锁选项有什么
- 50种方法巧妙优化你的SQLServer数据库