如何使用会话将登录和注销时间存储在数据库中 [英] how to store login and logout time in database using sessions
问题描述
大家好
您能发送代码如何使用会话在数据库中存储登录和注销时间吗,您能解决吗
下面
片段
SqlConnection con =新的SqlConnection(数据源= suhels;初始目录= suhel1;用户ID = sa;密码= abc");
SqlCommand cmd;
受保护的void Page_Load(对象发送者,EventArgs e)
{
}
受保护的void Login_Click(对象发送者,EventArgs e)
{
con.Open();
字符串q =从成员中选择Count(*),其中username =""+ TextBox1.Text +"''AND密码=" + TextBox2.Text +";
SqlCommand comm =新的SqlCommand(q,con);
int结果=(int)comm.ExecuteScalar();
如果(结果> 0)
{
字符串s =插入Logins(""+ TextBox1.Text +"'',DateTime.Now
cmd.Parameters.AddWithValue("@ username",TextBox1.Text);
cmd.Parameters.AddWithValue("@ logintime",DateTime.Now.ToShortDateString());
cmd.ExecuteNonQuery();
会话[用户"] = TextBox1.Text.ToString();
cmd =新的SqlCommand(s,con);
int r = cmd.ExecuteNonQuery();
}
hi to all
can u pls send the code how to store login and logout time in database using sessions can u solve dis
below
snippet
SqlConnection con = new SqlConnection("Data Source=suhels;Initial Catalog=suhel1;User ID=sa;Password=abc");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Login_Click(object sender, EventArgs e)
{
con.Open();
string q = "select Count(*) from memebers where username=''" + TextBox1.Text + "'' AND password =''" + TextBox2.Text + "''";
SqlCommand comm = new SqlCommand(q, con);
int result = (int)comm.ExecuteScalar();
if (result > 0)
{
String s = "insert into Logins(''"+TextBox1.Text+"'',DateTime.Now
cmd.Parameters.AddWithValue("@username",TextBox1.Text);
cmd.Parameters.AddWithValue("@logintime", DateTime.Now.ToShortDateString());
cmd.ExecuteNonQuery();
Session["User"] = TextBox1.Text.ToString();
cmd = new SqlCommand(s, con);
int r = cmd.ExecuteNonQuery();
}
推荐答案
您可以使用以下事件在数据库中存储dateTime
在LoginButton上单击-您可以存储 DateTime.Now.ToString("MM/dd/yyyy hh:MM:ss")
登出链接-在Application.Exit之前存储日期时间
这很简单,是什么问题?
You can use follwing events for storing dateTime in database
on LoginButton click - you can store DateTime.Now.ToString("MM/dd/yyyy hh:MM:ss")
on logout link - before Application.Exit store datetime
this is simple, what an issue ?
有一个链接描述了您真正想要的....
去吧.这是一篇很好的文章.
跟踪用户在您网站上的登录/注销时间 [ ^ ]
There is one link descibing what you really want....
Go for it. It is a very good article.
Tracking users login/logout times on your site[^]
这篇关于如何使用会话将登录和注销时间存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!