繁体   English   中英

如何显示“当前用户”的数据?

[英]How do I display the data of the “current user”?

我是一名高中生,仍然是C#的初学者。

我正在为用户创建一个包含数据库(visual studio(?)中的sql本地数据库)的图书馆管理系统(用于书籍)。 我有一个表单,其中用户可以在注册表单中查看他们输入的数据(用户ID,姓名,用户名,课程,部分)。 唯一的问题是它只显示创建的第一个帐户的数据。 无论我创建了多少其他帐户,它仍然只显示第一个帐户。 我如何制作它以显示登录的“当前”用户/帐户的数据?

我尝试通过更改稍微更改代码

SqlCommand cmd = conn.CreateCommand();
               cmd.CommandType = CommandType.Text;
               cmd.CommandText = "Select * from [tbl_accounts]";

string select = "Select * from [tbl_accounts]";
               SqlCommand cmd = new SqlCommand(select, conn);

虽然,我认为它们基本相同。 我真的不知道该怎么做,因为我发现的其他解决方案要复杂得多。

这是我现在使用的代码:

try
{
   SqlConnection conn = new SqlConnection(@"[connection string]");
   conn.Open();

   string select = "Select * from [tbl_accounts]";
   SqlCommand cmd = new SqlCommand(select, conn);
   SqlDataReader dr = cmd.ExecuteReader();

   if(dr.Read())
   {
       materialLabel6.Text = dr["accountID"].ToString();
       materialLabel7.Text = dr["username"].ToString();
       materialLabel8.Text = dr["name"].ToString();
       materialLabel9.Text = dr["strand"].ToString();
       materialLabel10.Text = dr["section"].ToString();
   }

}
catch (Exception ex)
{ 
   MessageBox.Show(ex.Message);}      
}

我希望看到的结果是例如:

用户(表):

  1. PERSONA
  2. PersonB

目前登录:PersonB

[人员数据]

所以这意味着表单只显示PersonB的数据而不是PersonA的数据

对于初学者,如果您需要多行数据,则需要遍历数据读取器中的所有行。 现在你只返回第一行。 此链接应包含相关信息。 但是,理想情况下,您需要从UI(或用于触发对函数的调用的任何内容)发送一个表示用户的参数(用户表中的ID或任何唯一字段)并将其发送到sql查询的where子句,以便您只提取所需的记录。

查询应该看起来像:

public void GetUserInfo(int userId) // though you might want to change the parameter to suit your needs. It's hard to tell without being able to see the schema
{
    string select = string.Format("Select * from [tbl_accounts] where Id = {0}", userId.ToString()); // something along these lines
    SqlCommand cmd = new SqlCommand(select, conn);

    SqlDataReader dr = cmd.ExecuteReader();

    if(dr.Read())
    {         
        materialLabel6.Text = dr["accountID"].ToString();
        materialLabel7.Text = dr["username"].ToString();
        materialLabel8.Text = dr["name"].ToString();
        materialLabel9.Text = dr["strand"].ToString();
        materialLabel10.Text = dr["section"].ToString();
    }
}

编辑:快速注释,如果您调整查询以便根据参数拉出一条记录,则不需要进行循环。

另一个快速编辑:我分解了代码,因此它更具可读性。 这更像是一种“理想的实现”,并强制执行一些更好的代码实践。 (我知道这是一个高中项目,但最好习惯于分解代码,因此它在imo早期更通用。这主要是为了可维护性。在大型项目中,保持一切紧密耦合在一起很难管理。)

public User GetUserInfo(int userId) // though you might want to change the parameter to suit your needs. It's hard to tell without being able to see the schema for the user table
{

    SqlConnection conn = new SqlConnection(@"[connection string]");
    conn.Open();

    string select = string.Format("Select * from [tbl_accounts] where Id = {0}", userId.ToString()); // something along these lines
    SqlCommand cmd = new SqlCommand(select, conn);

    SqlDataReader dr = cmd.ExecuteReader();

    User user = new User();

    if(dr.Read())
    {   
        user.AccountId = dr["accountID"].ToString();
        user.UserName = dr["username"].ToString();
        user.Name = dr["name"].ToString();
        user.Strand = dr["strand"].ToString();
        user.Section = dr["section"].ToString();
    }
    return user;
}

public void SetValues(User user) 
{
    materialLabel6.Text = user.AccountId;
    materialLabel7.Text = user.UserName;
    materialLabel8.Text = user.Name;
    materialLabel9.Text = user.Strand;
    materialLabel10.Text = user.Section;
}


public class User 
{
    string AccountId { get; set; }
    string UserName { get; set; }
    string Name { get; set; }
    string Strand { get; set; }
    string Section { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM