如何通过

时间:2016-09-26 07:25:37

标签: sql-server class methods get

我想在用户登录时存储系统的当前时间。

public string getlgn(string userNametime, string passWordtime)
{
    var myUser = entity.MVCUsers.FirstOrDefault(m => m.Username == userNametime && m.Password == passWordtime);
    //string s= lastweblogin(userNametime, passWordtime);

    // Get previous login
    var prevLogin = myUser.Lastlogintime;

    // Update with current login
    string currentime = DateTime.Now.ToString();
    prevLogin = currentime;
    MVCUser tbluser = new MVCUser();
    entity.MVCUsers.Add(tbluser);

    //entity.SaveChanges();
    var userlogindate = myUser.UserID.ToString();
    return (userlogindate);
}

2 个答案:

答案 0 :(得分:0)

您可以从sys.dm_exec_sessions表中检索用户的login_time信息。

例如:

select max (login_time) as last_login_time, login_name from sys.dm_exec_sessions group by login_name;

答案 1 :(得分:0)

请尝试使用以下代码替换您的代码。

使用日期时间变量而不是字符串,不要将其转换为.tostring

public string getlgn(string userNametime, string passWordtime)
{

    var myUser = entity.MVCUsers.FirstOrDefault(m => m.Username == userNametime && m.Password == passWordtime);
    //string s= lastweblogin(userNametime, passWordtime);

    // Get previous login
    var prevLogin = myUser.Lastlogintime;

    // Update with current login
    datetime currentime = DateTime.Now;
    prevLogin = currentime;
    MVCUser tbluser = new MVCUser();
    entity.MVCUsers.Add(tbluser);

    //entity.SaveChanges();
    var userlogindate = myUser.UserID.ToString();
    return (userlogindate);
}

或者您可以查看此方法以供参考

public ActionResult Index(UserLogin user)
{
    try
    {
        if (ModelState.IsValid)
        {
            Session["UserName"] = user.UserName.ToString();
            User us = new User();

            if (us.login(user))
            {
                int id = user.PK_UserID;
                user.LastLogin = DateTime.Now;
                us.updatelogintime(id,user);
                return RedirectToAction("AddUsers", "User");
            }
        }
        ViewBag.Message = "  Invalid Username or Password";
        return View();
    }
    catch
    {
        return View();
    }