记录用户上次登录时间

sky11007 2008-10-09 04:31:55
我想做一个就是上次访问者的登录时间和ip 我用了firsttime,lasttime ,firstip,lastip四个字段,我是让他在用户登录时候把这四个字段写入到数据库代码是
command1.CommandType = CommandType.StoredProcedure;
conn1.Open();
SqlParameter date = command1.Parameters.Add("@firsttime", SqlDbType.DateTime);
date.Value = Convert.ToString(DateTime.Now);
SqlParameter date1=command1 .Parameters .Add ("@lasttime",SqlDbType.DateTime );
date1.Value = date .Value ;
SqlParameter firstip = command1.Parameters.Add("@firstip", SqlDbType.NVarChar, 50);

firstip.Value = Convert.ToString(Request.UserHostAddress);
SqlParameter lastip=command1 .Parameters .Add ("@lastip",SqlDbType.NVarChar ,50);
lastip.Value = firstip.Value;
command1.ExecuteNonQuery();
conn1.Close();
用的是存储过程 但是写入数据库的时间firsttime和lasttime是一个样的,是同一时间,不知道怎样才能读出上次登录时间,
问题很简单,初学者,请详细讲解,谢谢
...全文
920 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhifeng2012 2010-07-14
  • 打赏
  • 举报
回复
ding!ding!ding!
js1722 2008-10-16
  • 打赏
  • 举报
回复
先取改用户的最后一次登陆时间嘛. 如果不存在的话,那他就是第一次登录了.select max(lasttime ) from table where id = '用户ID'
lovehongyun 2008-10-10
  • 打赏
  • 举报
回复
你把两个时间全搞成DateTime.Now那肯定两个时间都是相同的,而且是当前时间
lovehongyun 2008-10-10
  • 打赏
  • 举报
回复
你要取出上次登录的时间才行.

而不是把所有的时间都用当前时间
wihtsky 2008-10-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 bingbingw 的回复:]
firsttime 只在第一次时才是DateTime.Now。再插入time和ip时判断一下,以后每次再插入的firstime应该是上次的lasttime。除非你要保留firsttime这个时间。
[/Quote]
bingbingw 2008-10-10
  • 打赏
  • 举报
回复
firsttime 只在第一次时才是DateTime.Now。再插入time和ip时判断一下,以后每次再插入的firstime应该是上次的lasttime。除非你要保留firsttime这个时间。
coco_ke 2008-10-10
  • 打赏
  • 举报
回复
FirstTime和LastTime每次登陆时都更新时间,而且更新的都是当前时间,如果在这之后再读取FirstTime和LastTime,读出来的还是当前时间
sky11007 2008-10-09
  • 打赏
  • 举报
回复
谢谢 请大家多多指教
sky11007 2008-10-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 Aden 的回复:]
firsttime 只是第一次记录才写,你这里每次都写有问题.加个判断该用户是否已登录
[/Quote]
能不能在说的详细点啊,我的整体代码是这样的
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["project_DataConnectionString"].ConnectionString);
SqlCommand command = new SqlCommand("selectuser", conn);
command .CommandType =CommandType .StoredProcedure ;
conn.Open();
SqlParameter username = command.Parameters.Add("@name", SqlDbType.NVarChar, 50);
username.Value = TextBox1.Text;
SqlParameter pass = command.Parameters.Add("@pwd", SqlDbType.NVarChar, 50);
pass.Value = TextBox2.Text;
SqlDataReader dr = command.ExecuteReader();
if (dr.Read() == true)
{
Session["username"] = name;
SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["project_DataConnectionString"].ConnectionString);
SqlCommand command1 = new SqlCommand("insertlogin01",conn1 );
command1.CommandType = CommandType.StoredProcedure;
conn1.Open();
SqlParameter date = command1.Parameters.Add("@firsttime", SqlDbType.DateTime);
date.Value = Convert.ToString(DateTime.Now);
SqlParameter date1=command1 .Parameters .Add ("@lasttime",SqlDbType.DateTime );
date1.Value = date .Value ;
SqlParameter firstip = command1.Parameters.Add("@firstip", SqlDbType.NVarChar, 50);

firstip.Value = Convert.ToString(Request.UserHostAddress);

SqlParameter lastip=command1 .Parameters .Add ("@lastip",SqlDbType.NVarChar ,50);
lastip.Value = firstip.Value;
command1.ExecuteNonQuery();
conn1.Close();
Server.Transfer("kyfb.aspx");

}
else
{
Session["username"] = "";
this.Response.Write("<script>window.alert('用户名或密码错误')</script>");

}
dr.Close();


conn.Close();
Aden 2008-10-09
  • 打赏
  • 举报
回复
firsttime 只是第一次记录才写,你这里每次都写有问题.加个判断该用户是否已登录
yangpeiyu 2008-10-09
  • 打赏
  • 举报
回复
你上次的时间是必需先从你库里的firsttime这个字段的值..以此类推.

是先从库取出值然后分别赋给对应的.然后当前是永远是DateTime.Now

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧