如何显示用户的首次登录时间? [英] How to display the first login time for the user?

查看:77
本文介绍了如何显示用户的首次登录时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在SQL Server 2008和asp.net平台上工作.我的项目有一个按钮,单击该按钮即可显示用户的登录时间.
我需要这样一个代码,当用户在同一天第二次登录时,我们必须显示一个标签,显示您已登录于",并显示一个文本框,显示该日的首次登录时间.另外,该按钮也应在页面上禁用.

关于此的任何帮助将非常有用.

谢谢.

Hi to all,

I m working in SQL server 2008 and asp.net platform. My project has a button which on being clicked, shows the login time of the user.
I need a code as such that when the user logs in for the second time on the same day we have to display a label showing "You have logged in at" and a textbox displaying the first login time for that day. Also the button should be disabled on the page.

Any help regarding this would be very useful.

Thank you.

推荐答案

您应该具有Logindetails表.

您可以通过两种方式进行操作
第一种方式

每次用户登录时插入新记录,并获取特定日期的分钟(时间).
这是一种历史,因此您可以跟踪每次登录

第二种方式:

用户当天首次登录时插入新记录.如果用户尝试第二次登录,请去检查该特定日期和特定用户的记录是否已存在.如果存在,请不要插入.
因此,现在每个用户每天只有一个登录时间.没有历史记录跟踪.

You should have Logindetails table.

You can do this in 2 way
First way

Insert new record everytime the user logins and get the min(time ) for that particular day.
This is kind of history so you can keep track of every login

Second way:

Insert new record when user logins first time for the day. If user tries to login second time, go and check whether a record already exists for that particular day and for particular user . If it exists dont insert.
So now you will have only one login time per day per user. No history tracking.

if not exists(select 1 from  logintable
where CONVERT(varchar(8), logindate, 112) =CONVERT(varchar(8), GETDATE(), 112) and Id=1)
insert into logintable(col1,col2,col3,logindate) values (val1,val2,val3,getdate())



根据评论更新了解决方案.



updated solution, based on the comments.

protected void Button2_Click(object sender, EventArgs e)
    {
        string intime = DateTime.Now.ToString();

        string ID = Session["Name"].ToString();
        string indate = DateTime.Now.ToShortDateString();
        SqlConnection cn = new SqlConnection("Data Source=Sony-VAIO\\MSSQLSERVER2008;InitialCatalog=db;Integrated Security=True");
        cn.Open();
        SqlCommand cmd = new SqlCommand(@"if not exists (select 1 from intime where convert(varchar(30),indate,112)=convert(varchar(30),getdate(),112) and Id=@Id) insert into intime
values ('" + ID + "','" + intime + "','" + indate + "')", cn);
        cmd.Parameters.AddWithValue("@Id", ID);

        cmd.ExecuteNonQuery();
        cn.Close();


    }


步骤1:
在每次登录时更新ur表,logtime列

步骤2:
将该日期与当前日期进行比较,
如果与检索日期和时间匹配,并显示在文本框中
也禁用按钮
否则,
什么都不检索
step 1:
update ur table, logtime column on every login

step 2:
compare that date with the current date,
if it matches retrive date and time and show it in textbox
also disable the button
otherwise,
retrieve nothing


您可以将首次登录时间存储在数据库中.
以下是一些示例-
跟踪用户在您网站上的登录/注销时间 [ ^ ]
http://www.4guysfromrolla.com/articles/081507-1.aspx [ ^ ]
You can store the first login time in a database.
Here are some examples -
Tracking users login/logout times on your site[^]
http://www.4guysfromrolla.com/articles/081507-1.aspx[^]


这篇关于如何显示用户的首次登录时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆