我希望节省用户在数据库中从头到尾花费多少时间 [英] I want save time how much user spend time from start to end in database
本文介绍了我希望节省用户在数据库中从头到尾花费多少时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试计算开始和结束时间,并将其保存到用户在数据库中的总列消耗量,并且它正在保存类似物
I'm Trying to calculate start and end time and save it to total column spend by the user in data base and it is Saving Like
0:0:0
以下是我的代码
提前感谢
and following is my code
thanks in advance
mydb db = new mydb();
string st = "";
DateTime dt1 = DateTime.Now;
DateTime dt = DateTime.Now;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
st = "insert into tbl_time(time_in) values('" + dt.ToString("hh:mm:ss") + "')";
db.ExeQuery(st);
}
}
protected void btn_save_Click(object sender, EventArgs e)
{
TimeSpan ts = dt1 - dt;
st = "update tbl_time set time_out='" + dt1.ToString("hh:mm:ss") + "',tot='" + ts + "'";
int x = db.ExeQuery(st);
if (x > 0)
{
Response.Write("Saved");
}
else
{
Response.Write("NOt Savd");
}
}
推荐答案
不要将值连接到SQL语句,而是使用的SqlParameter [ ^ ]
另一件事是你初始化dt1和dt为相同的值,永远不会改变它们。也许代码应该看起来像
Do not concatenate values to your SQL statement, instead use SqlParameter[^]
The other thing is that you initialize the dt1 and dt to the same value and never change them. Perhaps the code should look like
...
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
st = "insert into tbl_time(time_in) values('" + dt.ToString("hh:mm:ss") + "')";
db.ExeQuery(st);
dt1 = DateTime.Now;
}
}
...
嗯,实际上参数的代码看起来应该是这样的。
Well, actually with parameters the code should look like
...
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
st = "insert into tbl_time(time_in) values(@timevalue)";
// here goes the code that sets the value of parameter @timevalue
db.ExeQuery(st);
dt1 = DateTime.Now;
}
}
...
您可以拥有包含Session_Start,Session_End,Total_Time_On_MY_Site列的表。
在会话开始时插入当前时间。会话结束时也一样。然后从上面计算Total_Time_On_MY_Site两次。
你想为访客用户花时间吗?
如果是,那么当用户会话开始时获取DateTime.Now();然后在离开页面上添加到每个页面的Javascript,以了解用户离开的确切时间..你做的数学:)
You can have table having columns Session_Start,Session_End,Total_Time_On_MY_Site.
Insert current time when session starts. Do same on session End. And then calculate Total_Time_On_MY_Site from above two times.
Do you want time spend for guest user??
If yes then when the user session starts get DateTime.Now(); then add to each page Javascript on leave page to know the exact time the user left.. u do the math :)
我在Timspan得到的结果与0:0:0相同即ts
Im getting the same result like 0:0:0 in the Timspan i.e ts
这篇关于我希望节省用户在数据库中从头到尾花费多少时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文