我本来想实现的是点击comboBox中的内容,然后有几个textBox显示数据库中相应的东西,但是,当sql查询语句加了where之后,根本没数据进来,reader的HasRows都是false,不知道为啥,但是去掉where条件,才可以读取数据。
private void comboBox17_SelectedIndexChanged(object sender, EventArgs e)
{
projectname1 = comboBox1.Text;
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + @"\" + projectname1 + ".accdb;Persist Security Info=False;");
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from 桩位设计数据 where 孔号 ='"+comboBox17.Text+"'",con);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
textBox13.Text = reader["目标设计深度"].ToString(); textBox17.Text = reader["桶体长L"].ToString(); textBox19.Text = reader["桶体厚度t"].ToString(); textBox21.Text = reader["水位埋深"].ToString(); textBox15.Text = reader["清淤厚度"].ToString();
textBox14.Text = reader["分层高度"].ToString(); textBox18.Text = reader["桶体直径D"].ToString(); comboBox18.Text = reader["桶体个数"].ToString(); textBox20.Text = reader["泥面高度"].ToString(); textBox16.Text = reader["桶体重量"].ToString();
}
reader.Close();
con.Close();
}
根据你提供的代码和信息分析,可能是因为你的where条件不符合数据库中的数据,导致查询到的结果为空,从而OleDbdataReader无法读取内容。建议你检查一下comboBox17中选中的项是否与数据库中的数据一致,或者尝试手动输入一个已经存在的孔号,检查是否能够查询到数据。如果问题依然存在,可以尝试使用Debug模式逐行调试代码,找出具体出错的原因。