且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从数据库中检查用户的登录时间?

更新时间:2023-10-08 09:17:22

那是因为你正在检查(DateTime.Now).ToString(),这将是总是不同,因为它是 DateTime 值,而不仅仅是日期



要仅获得日期,您只需存储日期并进行比较。

您可以使用 DateTime.Now.ToString(M / d / yyyy);



但请确保在存储时,您还以此格式存储日期



了解更多关于格式,请参阅 - 自定义日期和时间格式字符串 [ ^ ]。


I want to save login and logout time of user in the databse.I am able to save login time.I want if user has logged in once there should not be duplicate entry on whole particular date. following are my sql coding and c# coding can any one help me..
thanx in advance
my sql table structure is as follows

create table EmpAttendance
(
    AttendenceID int primary key ,
    EmpID nvarchar(10) foreign key references Employee(EmpID),
    LoginTime datetime,
    LogoutTime datetime,
    AttDate datetime
)


And C# coding are..

DataSet ds1 = obj.CheckAttendance(Session["EmpID"].ToString(), (DateTime.Now).ToString());

if (ds1.Tables[0].Rows.Count > 0)
{
    //Response.Write("hiiiiii");
}
else
{
    obj.InsAttendance(Session["EmpID"].ToString(), (DateTime.Now).ToString(), "", (DateTime.Now).ToString());
    // Response.Write("hiiiiii");
}


every time only else part is running.....i want that else part should run only when there is no entry.....


[Edit member="Tadit"]
Added pre tags.
[/Edit]

That is because you are checking with (DateTime.Now).ToString(), which will be different always as it is a DateTime value, not Date only.

To get Date only, you have to store the Date only and compare that.
You can use DateTime.Now.ToString("M/d/yyyy");.

But make sure, while storing, you also store Date in this format.

To know more on formats, see - Custom Date and Time Format Strings[^].