使用 SQLDataReader 检索数据库值并在表单上显示它们

Retrieve Database values and show them on form using SQLDataReader

我正在尝试从数据库中检索数据并将它们显示在表单上;但是我的代码不工作......我没有错误,并且逻辑上它似乎工作(对我来说)所以我无法弄清楚我哪里出了问题。这就是我需要你帮助的地方!

private void tableListBox_SelectedIndexChanged(object sender, EventArgs e)
{

    string constring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\IncomerDefault.mdf;Integrated Security=True;Connect Timeout=30";
    string Query = "SELECT * FROM [Table] WHERE Default_Name = '" + tableListBox.SelectedValue + "'";
    SqlConnection con = new SqlConnection(constring);
    SqlCommand cmd = new SqlCommand(Query, con);
    SqlDataReader Reader;
    try
    {
        con.Open();
        Reader = cmd.ExecuteReader();

        while (Reader.Read())
        {
                textBox1.Text = Reader.GetValue(2).ToString();
                comboBox1.Text = Reader.GetValue(3).ToString();
                comboBox3.Text = Reader.GetValue(4).ToString();
                textBox2.Text = Reader.GetValue(6).ToString();
                comboBox2.Text = Reader.GetValue(7).ToString();
                comboBox4.Text = Reader.GetValue(8).ToString();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    con.Close();
}

'tableListBox' 填充了列 'Default_Name' 中的所有值。我想要它,以便当从列表框中选择 'Default_Name' 时,它会在文本框和组合框中显示与数据库中该行对应的值。

任何帮助都将不胜感激。谢谢

我将首先稍微更改您的设计,并建议您考虑使用数据表,然后只从数据表中检索行。

private void tableListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    private DataTable dataTable;
    string constring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\IncomerDefault.mdf;Integrated Security=True;Connect Timeout=30";
    string Query = "SELECT * FROM [Table] WHERE Default_Name = '" + tableListBox.SelectedValue + "'";
    SqlConnection con = new SqlConnection(constring);
    SqlCommand cmd = new SqlCommand(Query, con);
    try
    {
         con.Open();

         SqlDataAdapter da = new SqlDataAdapter(cmd);
         da.Fill(dataTable);

         foreach(DataRow row in dataTable.Rows)
         {
              textBox1.Text = row[2].ToString();
              comboBox1.Text = row[3].ToString();
              comboBox3.Text = row[4].ToString();
              textBox2.Text = row[6].ToString();
              comboBox2.Text = row[7].ToString();
              comboBox4.Text = row[8].ToString();
        }
        da.Dispose();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    con.Close();
 }

我通常发现 DataTables 比遍历实际 reader 更可靠。当然,这假设有数据被返回。也尝试将您的 select 语句更改为此

string Query = "SELECT * FROM [Table]"

如果可行,那么问题可能是

  1. 没有指定值的默认名称或
  2. tableListBox.SelectedValue 没有返回任何值,在这种情况下,请查看您的列表框 selected value

感谢 Takarii 的帮助。我想出了让谁让它发挥作用。

private void tableListBox_SelectedValueChanged(object sender, EventArgs e)
{
    string constring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\IncomerDefault.mdf;Integrated Security=True;Connect Timeout=30";
    string Query = "SELECT * FROM [Table] WHERE ID = '" + tableListBox.SelectedIndex.ToString() + "'";
    SqlConnection con = new SqlConnection(constring);
    SqlCommand cmd = new SqlCommand(Query, con);
    SqlDataReader Reader;
    try
    {
        con.Open();
        Reader = cmd.ExecuteReader();

        while (Reader.Read())
        {
            textBox1.Text = Reader.GetValue(2).ToString();
            comboBox1.Text = Reader.GetValue(3).ToString();
            comboBox3.Text = Reader.GetValue(4).ToString();
            textBox2.Text = Reader.GetValue(6).ToString();
            comboBox2.Text = Reader.GetValue(7).ToString();
            comboBox4.Text = Reader.GetValue(8).ToString();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    con.Close();
}

首先,我将 void 更改为 'SelectedValueChanged',然后我将连接查询中的 'WHERE' 更改为与所选值关联的行索引。

感谢大家的帮助!