如何显示"current user"的数据?



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

我正在制作一个图书馆管理系统(用于书籍(,其中包含用于用户的数据库(Visual Studio(?(中的SQL本地数据库(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. 角色
  2. Personb

目前登录: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在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; }
}

相关内容

最新更新