用户登录历史记录出现问题 [英] Problem with users login history

查看:78
本文介绍了用户登录历史记录出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我有一个Web应用程序项目,该项目用于显示用户的登录和注销历史记录.

在我的Web窗体上,我有一个Gridview,用于显示用户登录历史记录.

我使用了一种方法来在gridview中显示数据并将其绑定....它显示得很好.
我没有使用Gridview边界域来显示数据.

在此Gridview中,我使用了以下字段作为用户ID,用户名,login_time,logout_time,并且我还需要将从登录到注销的持续时间显示为2小时,如下所示.

正在显示登录和注销之间的时间...但是它不会在Gridview中显示列标题作为持续时间.

我在数据库中使用了持续时间字段".但是无法显示.

这是我的代码.


Dear Friendss,

I have a Webapplication project, in that i used to display users login and logout history.

On my webform I have a Gridview which is used to display Users Login History.

I used a Method to display the data in gridview and binded it....its displaying fine.
I didnt use Gridview boundfields to display the data.

In this Gridview I used fields as userid,username, login_time, logout_time and also i need to display the Duration time from login to logout as 2 hours something like this.

the time between login and logout is displaying...but it doesnt show column header in Gridview as duration.

I took Duration Field in Database....but unable to display.

This is my code.


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Users_UserLoginHistory : System.Web.UI.Page
{
    string conString = ConfigurationManager.ConnectionStrings["infosystemDBConnectionString"].ToString();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridData();
        }

    }
    protected void BtnGo_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(conString);


        //SqlCommand cmd = new SqlCommand("Select LogId,UserName,Login_Time,Logout_Time from UserLogs where UserName='" + TxtSearch.Text + "' order by Login_Time desc", con);
        SqlCommand cmd = new SqlCommand("Select LogId,UserName,Login_Time,Logout_Time,convert(nvarchar,datediff(mi,Login_Time,Logout_time))) + ' Mins ' from UserLogs where UserName='" + TxtSearch.Text + "' order by Login_Time desc", con);
        

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        Grid1.DataSource = dt;
        Grid1.DataBind();
    }

    private void GridData()
    {
        SqlConnection con = new SqlConnection(conString);

        SqlCommand cmd = new SqlCommand("Select LogId,UserName,Login_Time,Logout_Time from UserLogs order by Login_Time desc", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        Grid1.DataSource = dt;
        Grid1.DataBind();

    }
}




请解决我的问题,并给我一个完美的解决方案.我需要标题作为持续时间.

在此先感谢.

[edit]添加了代码块-OriginalGriff [/edit]




Please solve my problem and give me a perfect solution. i need header as duration.

Thanks in Advance.

[edit]Code block added - OriginalGriff[/edit]

推荐答案

两件事:
1)假设
Two things:
1) Assuming that
convert(nvarchar,datediff(mi,Login_Time,Logout_time))) + ' Mins '

是您要作为持续时间的列,只需添加"AS Duration "的结尾:

is the column you want as the duration, just add "AS Duration" to the end of it:

Select LogId,UserName,Login_Time,Logout_Time,convert(nvarchar,datediff(mi,Login_Time,Logout_time))) + ' Mins ' from UserLogs

成为

Select LogId,UserName,Login_Time,Logout_Time,convert(nvarchar,datediff(mi,Login_Time,Logout_time))) + ' Mins ' AS Duration from UserLogs



2)不要连接字符串以构建SQL命令.它使您对意外或蓄意的SQL注入攻击敞开大门,这可能会破坏整个数据库.请改用参数化查询.以这种方式构建命令使我可以从世界任何地方连接到您的站点,并通过大约二十次简单的击键删除整个数据库.



2) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. Building your command that way allows me to connect to your site from anywhere in the world and delete your entire database in about twenty simple keystrokes.


这篇关于用户登录历史记录出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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