温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Asp.net根据IP显示省市拼音源码
当前文件:
IpCity/App_Code/IPAccess.cs,打开代码结构图
IpCity/App_Code/IPAccess.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Web; 5
using System.Web.Security; 6
using System.Web.UI; 7
using System.Web.UI.WebControls; 8
using System.Web.UI.WebControls.WebParts; 9
using System.Web.UI.HtmlControls; 10
using System.Data.OleDb; 11
12
/// <summary> 13
/// IPAccess 的摘要说明 14
/// </summary> 15
/// 16
/// Download from www.51aspx.com(51aspx.com) 17
18
[System.ComponentModel.DataObject] 19
public class IPAccess 20
{ 21
public static readonly string CONNSTRING=System.Configuration.ConfigurationManager.AppSettings["IPConnString"].ToString()+System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"])+";"; 22
public static readonly string COMMANDTEXT = "Select top 1 city,province from ip_address where "; 23
public static readonly string selectcityname = "Select pinyin from city_py Where city="; 24
25
26
public IPAccess() 27
{ 28
// 29
// TODO: 在此处添加构造函数逻辑 30
// 31
} 32
33
// 获取客户端所在城市,省份,跳转域名 34
public string[] GetCustomCity() 35
{ 36
37
string Ip = GetClientIP(); 38
string[] ipcontent = new string[3]; 39
try 40
{ 41
long ipint = IPtoNum(Ip); 42
43
OleDbConnection oleconn = new OleDbConnection(); 44
oleconn.ConnectionString = CONNSTRING; 45
46
OleDbCommand OleCommand = new OleDbCommand(); 47
OleCommand.Connection = oleconn; 48
49
OleCommand.CommandText = COMMANDTEXT + ipint.ToString() + ">=ip1 and " + ipint.ToString() + "<=ip2 order by id desc"; 50
51
oleconn.Open(); 52
53
OleDbDataReader reader = OleCommand.ExecuteReader(); 54
55
if (reader.HasRows) 56
{ 57
reader.Read(); 58
ipcontent[0] = reader["city"].ToString(); 59
ipcontent[1] = reader["province"].ToString(); 60
} 61
else 62
{ 63
ipcontent[0] = ""; 64
ipcontent[1] = ""; 65
} 66
oleconn.Close(); 67
68
OleCommand.CommandText = selectcityname + "'" + ipcontent[0].ToString() + "'"; 69
70
oleconn.Open(); 71
reader = OleCommand.ExecuteReader(); 72
73
if (reader.Read()) 74
{ 75
ipcontent[2] = reader["pinyin"].ToString(); 76
} 77
else 78
{ 79
ipcontent[2] = ":该客户端的IP地址信息在数据库中没有找到相关信息"; 80
} 81
} 82
catch(Exception ex) 83
{ 84
ipcontent[0] = ex.Message.ToString() ; 85
ipcontent[1] = ex.Source.ToString(); 86
ipcontent[2] = ex.ToString(); 87
} 88
return ipcontent; 89
} 90
91
92
//将IP 地址转化为数字 93
public long IPtoNum(string Ip) 94
{ 95
string[] stringip = new string[4]; 96
stringip = Ip.Split('.'); 97
long ipnum = Convert.ToInt64((stringip[0])) * 16777216 + Convert.ToInt64(stringip[1]) * 65536 + Convert.ToInt64(stringip[2])*256 + Convert.ToInt64(stringip[3]); 98
return ipnum; 99
} 100
//获取客户端的ip地址 101
public string GetClientIP() 102
{ 103
string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 104
if (null == result || result == String.Empty) 105
{ 106
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 107
} 108
if (null == result || result == String.Empty) 109
{ 110
result = HttpContext.Current.Request.UserHostAddress; 111
} 112
return result; 113
} 114
} 115








