C#如何从数据表中按ID获取两列值
我对数据库和SQL没有太多的知识,所以我在这里尝试寻求帮助,我知道这应该是基本的知识,但作为一个新手,我不知道从哪里开始 我在计算如何从datatable中获取特定值时遇到问题,我已经在form2中使用了C#如何从数据表中按ID获取两列值,c#,sql-server,winforms,datatable,datatables,C#,Sql Server,Winforms,Datatable,Datatables,我对数据库和SQL没有太多的知识,所以我在这里尝试寻求帮助,我知道这应该是基本的知识,但作为一个新手,我不知道从哪里开始 我在计算如何从datatable中获取特定值时遇到问题,我已经在form2中使用了ID,使用以下代码: cmd = new SqlCommand("select UserID from Users where UserName='" + textBox1.Text + "' and UserPassword='&q
ID
,使用以下代码:
cmd = new SqlCommand("select UserID from Users where UserName='" + textBox1.Text + "' and UserPassword='" + textBox2.Text + "'", cn);
object result = cmd.ExecuteScalar();
if (result != null)
{
loginresult = 1;
LoginUserID = Convert.ToInt32(result);
AdminRights = ???; //how can I get this value from datatable?
UserRights = ???; //how can I get this value from datatable?
this.Close();
}
现在在form1中,我想通过ID获取AdminRights或UserRights的值,我有以下代码:
private void button7_Click(object sender, EventArgs e) // Button to Form2
{
Form2 win_form2 = new Form2();
win_form2.ShowDialog();
if (win_form2.loginresult == 1)
{
label4.Text = string.Format("User ID: {0}", win_form2.LoginUserID.ToString()); // Gets and places specific user ID
if (win_form2.UserRights = 0 && win_form2.AdminRights = 1) // Check if the ID has AdminRights
label5.text = string.Format("Rights: {0}", "Admin") // If true, do the following
else if (win_form2.UserRights = 1 && win_form2.AdminRights = 0) // Check if the ID has UserRights
label5.text = string.Format("Rights: {0}", "User") // If true, do the following
}
}
我的数据表:
- 用户ID(主键1,1)
- 用户名(字符串)
- 用户密码(字符串)
- AdminRights(int)(值可以是0或1,并且不能与UserRights的值相同)
- UserRights(int)(值可以是0或1,并且不能与AdminRights的值相同)
注意:我知道我有关于密码和SQL注入的非常危险的代码,现在我只想让它工作。您可以按名称访问列,如下所示,使用或使用数据表(但请注意,您应该使用命令参数,不要直接从用户输入将任何值放入查询中)
您可以使用下面的示例直接访问SQl列 例如:
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("Select ....", con)) //Your SQL Query here
{
con.Open();
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
if (dr != null)
{
while (dr.Read())
{
employee.AdminRights = dr["AdminRights"].ToString(); //This basically reads the SQL column into an "active reader"
employee.UserRights = dr["UserRights"].ToString();
}
}
}
con.Close();
}
另一种方法可能是在Windows窗体中使用并添加它们作为参数
警告:第一个代码块中的代码极易受到注入攻击!您需要将查询参数化。您的查询还暗示您正在存储纯文本密码;这也是一个巨大的问题。您应该将密码存储为散列值和盐值;从来不是纯文本。我知道它,但是现在我希望它能工作,我仍然试图全面理解SQL。您需要使用
ExecuteReader
或Datatable。Load
测试了它,完成了工作,谢谢。
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("Select ....", con)) //Your SQL Query here
{
con.Open();
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
if (dr != null)
{
while (dr.Read())
{
employee.AdminRights = dr["AdminRights"].ToString(); //This basically reads the SQL column into an "active reader"
employee.UserRights = dr["UserRights"].ToString();
}
}
}
con.Close();
}
cmd.Parameters.Add(new SqlParameter("@AdminUser", empInfo.AdminUser));
cmd.Parameters.Add(new SqlParameter("@UserRights", empInfo.UserRights));