C# 我如何显示";当前用户;?
我是一名高中生,在C#方面基本上还是个初学者 我正在为用户制作一个图书馆管理系统(用于图书),其中包括一个数据库(VisualStudio(?)中的sql本地数据库)。我有一个表单,用户可以在其中查看他们在注册表单中输入的数据(userID、name、username、course、section)。唯一的问题是,它只显示创建的第一个帐户的数据。无论我创建了多少个其他帐户,它仍然只显示第一个帐户。如何使其显示登录的“当前”用户/帐户的数据 我试着通过改变代码来稍微改变代码C# 我如何显示";当前用户;?,c#,visual-studio,local-database,C#,Visual Studio,Local Database,我是一名高中生,在C#方面基本上还是个初学者 我正在为用户制作一个图书馆管理系统(用于图书),其中包括一个数据库(VisualStudio(?)中的sql本地数据库)。我有一个表单,用户可以在其中查看他们在注册表单中输入的数据(userID、name、username、course、section)。唯一的问题是,它只显示创建的第一个帐户的数据。无论我创建了多少个其他帐户,它仍然只显示第一个帐户。如何使其显示登录的“当前”用户/帐户的数据 我试着通过改变代码来稍微改变代码 SqlCommand
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from [tbl_accounts]";
进入
虽然,我认为他们基本上是一样的。我真的不知道该怎么办,因为我发现的其他解决方案要复杂得多
这是我现在正在使用的代码:
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);}
}
我希望看到的结果是,例如:
用户(表):
这意味着表单将只显示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早期更通用。这主要是为了可维护性。在更大的项目中,将所有内容紧密结合在一起很难管理。)
请问公共课是做什么用的?对不起,我还不太熟悉其他术语。。。谢谢你!别担心。这只是一个示例函数,它返回一个可能的类,您可以将该类称为
User
。就我个人而言,我会添加该类并为用户表中的所有列添加成员,然后用DataReader
结果填充User
对象,然后将该对象发送回UI,这样MaterialLabel
就可以由该对象的值来设置。@Dortimer dude,您给了name变量returnValue,但在Read()中您可以填写用户并返回用户。您可以修改它以获得更清晰的代码吗?@Dortimer没关系)我只是想改进您的答案)用户类(public class User
)应该具有与您引用的SQL表中的数据类型(string
、int
、double
)匹配的字符串或变量。第二个示例中的顶部代码块:public User GetUserInfo(int userId)
是一个方法,它采用一个参数,通过数据库中的Id查找用户的数据,并返回一个用户对象。如果数据库没有“Id”列,则需要更改参数以查找唯一的用户数据(有时方法被称为函数,但现在“method”更为常见,这些术语或多或少都有相同的含义)
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();
}
}
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; }
}