如何根据当前登录用户显示数据? [英] How to display data based on current logged user?

查看:119
本文介绍了如何根据当前登录用户显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示根据当前登录用户从数据库中检索的数据?我知道我应该声明id,但我真的不知道如何... 

我在SQL中有2个表。

用户:
ID ||名字|| Last_Name ||日期||
列出
ID ||产品||描述|| user_id





我尝试过:



< pre> private void BindGridView()
{
string constr = ConfigurationManager.ConnectionStrings [strConn]。ConnectionString;

使用(SqlConnection conn = new SqlConnection(constr))
{

using(SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = @SELECT * From List where ID = @ID;
cmd.Parameters.AddWithValue(@ ID,?);

使用(SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
ad.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}

解决方案

成功登录后存储用户在会话中输入id以了解您的应用程序的已登录用户。



ex:会话[UserID] = UserID; (此处UserID是该会话的已记录用户ID)



根据您的问题,您希望通过记录来检索列表中的数据

cmd.CommandText = @SELECT * From List where ID = @ID;

Parameters.AddWithValue(@ ID,?);



问号的Instade传递记录的用户ID的会话值。

就像:

Parameters.AddWithValue(@ ID,Session [UserID ]);



如果UserID是一个整数字段,那么在传递之前作为参数将其转换为整数字段,如。 Parameters.AddWithValue(@ ID,Convert.ToInt32(Session [UserID]));



试一试。我认为这将解决您的问题。


我们无法明确告诉您该怎么做 - 我们不知道您的其余代码是如何工作的。但是,您所要做的就是查看用户登录的位置,并找到存储ID的位置。然后将该值替换为此行中的问号:

 cmd.Parameters.AddWithValue(@ ID,?); 


How do you display data that is retrieved from database according to the current login user? I know that I should declare the id, but I dont really know how...

I have 2 tables in SQL. 

User: 
ID || Name || Last_Name || date ||
List
ID || Product || description || user_id



What I have tried:

<pre>private void BindGridView()
    {  
string constr = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(constr))
            {

                using (SqlCommand cmd = new SqlCommand())
                { 
                    cmd.Connection = conn;
                    cmd.CommandText = @"SELECT * From List where ID = @ID";
                    cmd.Parameters.AddWithValue("@ID", ?);

                    using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        ad.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }     
        }
    }

解决方案

After successful login store the user id in in a session to know the logged user for you application.

ex: Session["UserID"]= UserID; (Here UserID is the logged user id for that session)

As per your question you want to retrieve the data from list by of a logged
cmd.CommandText = @"SELECT * From List where ID = @ID";
Parameters.AddWithValue("@ID", ?);

Instade of question mark pass the session value of logged user id.
Just like :
Parameters.AddWithValue("@ID", Session["UserID"]);

If UserID is an integer field then before pass as parameter convert it to integer field like. Parameters.AddWithValue("@ID", Convert.ToInt32(Session["UserID"]));

Try it. I think it will solve your problem.


We can't tell you explicitly what to do - we don't have any idea how the rest of your code works. But all you have to do is look at where you log the user in, and find where you store the ID. Then put that value in place of the question mark in this line:

cmd.Parameters.AddWithValue("@ID", ?);


这篇关于如何根据当前登录用户显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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