頁面執行
///获取用户浏览器信息
HttpBrowserCapabilities browser = Request.Browser;
///判断用户是否已经访问
if (Session["Tdepot"] == null)
{
try
{ ///定义对象
///执行数据库操作
///
visitor.AddVisitor(Request.UserHostAddress, browser.Platform, browser.Browser,
browser.Version, Request.UserLanguages[0].ToString(), browser.Cookies, "AERO首頁");
///添加已经访问的标志
Session["Tdepot"] = "Tdepot";
}
catch (Exception ex)
{ ///跳转到异常错误处理页面
Response.Redirect("404.htm");
}
}
類文件
public int AddVisitor(string sIP,string sOS,string sBrowser,string sVision,string sLanguage,bool bIsCookie,string from)
{
///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["EPS_WEBConnectionString"].ConnectionString);
///定义SQL语句
string cmdText = "INSERT INTO webcount(IP,OS,Browser,Vision,Language,IsCookie,[from])VALUES("
+ "'" + sIP + "',"
+ "'" + sOS + "',"
+ "'" + sBrowser + "',"
+ "'" + sVision + "',"
+ "'" + sLanguage + "',"
+ "'" + (bIsCookie == true ? 1 : 0).ToString() + "',"
+ "'" + from +"' ) ";
///创建Command
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
// DataSet ds = Cds("select top 1 * from server_IP order by create_date desc");
///定义返回值
int nResult = -1;
if (sIP.Substring(0, 3) != "192" && sIP.Substring(0, 3) != "127") //&& sIP != ds.Tables[0].Rows[0]["server_ip"].ToString ())
try
{
///打开链接
myConnection.Open();
///执行SQL语句
nResult = myCommand.ExecuteNonQuery();
}
catch(SqlException ex)
{
///抛出异常
throw new Exception(ex.Message,ex);
}
finally
{ ///关闭链接
myConnection.Close();
}
///返回nResult
return nResult;
}