【问题标题】:C# how to get two column values by ID from a datatableC#如何通过ID从数据表中获取两列值
【发布时间】:2021-04-14 03:18:13
【问题描述】:

我对数据库和SQL不太了解,所以我想在这里寻求帮助,虽然我知道这应该是基本知识,但作为一个新手我不知道从哪里开始。

我在弄清楚如何从数据表中获取特定值时遇到问题,我已经使用以下代码在 form2 中拥有 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 (PK 1,1)
  • 用户名(字符串)
  • 用户密码(字符串)
  • AdminRights (int)(值可以是 0 或 1,并且不能与 UserRights 的值相同)
  • UserRights (int)(值可以是 0 或 1,并且不能与 AdminRights 的值相同)

那么最后,我如何访问数据表并在 form2 中获取 UserRights 和 AdminRights 值?

注意:我知道我有关于密码和 SQL 注入的非常危险的代码,现在我只想让它工作。

【问题讨论】:

  • 警告:您的第一个代码块中的代码很容易受到注入攻击!您需要为您的查询参数化。此外,您的查询暗示您正在存储纯文本密码;这也是一个巨大的担忧。您应该将密码存储为散列和加盐值; 从不作为纯文本。
  • 我知道这一点,但是现在我希望它能够工作,我仍在尝试全面了解 SQL。
  • 您需要使用ExecuteReaderDatatable.Load

标签: c# sql-server winforms datatable datatables


【解决方案1】:

您可以按名称访问列,如下所示,使用schema 或使用 ie 数据表(但请注意,您应该使用命令参数,不要直接从用户输入中将任何值放入查询中)

using (SqlConnection connection = new SqlConnection(
           connectionString))
{
    connection.Open();

    SqlCommand command = new SqlCommand($"SELECT * FROM TABLE_NAME WHERE UserID = {your_user_id}", connection);
    SqlDataReader reader = command.ExecuteReader();
    while (_reader.Read())
    {
      // get the results of each column
      var adminRights =  reader["AdminRights "] as string;
      var userRights = reader["AdminRights "] as string;
    }
}

// usage with parameters
SqlCommand command = new SqlCommand("SELECT * FROM TABLE_NAME WHERE UserID =@user", sql);
command.Parameters.AddWithValue("@user", your_user_id);

【讨论】:

    【解决方案2】:

    您可以使用以下示例直接访问 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();
    }
    

    另一种方法可能是使用variables in SQL 并将它们作为参数添加到Windows 窗体中。 Read this answer

    cmd.Parameters.Add(new SqlParameter("@AdminUser", empInfo.AdminUser));
    cmd.Parameters.Add(new SqlParameter("@UserRights", empInfo.UserRights));
    

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多