检索数据库值并使用SQLDataReader在表单上显示它们
本文关键字:表单 显示 SQLDataReader 数据库 检索 | 更新日期: 2023-09-27 18:19:43
我正试图从数据库中检索数据并将其显示在表单上;但我的代码不起作用。。。我没有错,从逻辑上讲,这似乎(对我来说)有效,所以我不知道我哪里错了。那就是我需要你帮助的地方!
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比在实际读取器中循环更可靠。当然,这是假设有数据被返回。也可以尝试将您的选择语句更改为此
string Query = "SELECT * FROM [Table]"
如果这有效,那么问题可能是
- 没有指定值的默认名称或
- tableListBox.SelectedValue没有返回任何值,在这种情况下,请查看您的列表框所选值
感谢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"更改为与所选值关联的行索引。
感谢大家的帮助!