当我尝试执行下面显示的 SQL 查询时,我得到一个 IndexOutOfRangeException。我不知道为什么会这样,在其他 SO 页面上它说这可能是因为您尝试从不存在的字段中获取数据,但我确定它存在并且当我将两个请求的字段都从“ ADRES" 和 "TAAL" 到 "LEV" 就像它们上面的一样,只有底部的 2 个会拒绝工作,而对 "LEV" 的最高请求仍然有效。“ADRES”是一个 8 长的 varchar,“TAAL”是一个 1 长的 varchar 字段
try
{
//BESTEL,[PLAN],LEV,ADRES,TAAL
SqlCommand getlist = new SqlCommand("select * from BESW where BEST=@best", Connectie.connMEVO);
getlist.Parameters.Add("@best", SqlDbType.VarChar).Value = data.corrigeerbestnr;
DRorder = getlist.ExecuteReader();
while (DRorder.Read())
{
dateTimePicker1.Value = Convert.ToDateTime(DRorder["BESTEL"]);
dateTimePicker2.Value = Convert.ToDateTime(DRorder["PLAN"]);
comboBox1.Text = DRorder["LEV"].ToString();
comboBox2.Text = DRorder["ADRES"].ToString();
textBox8.Text = DRorder["TAAL"].ToString();
}
}
catch (Exception er) { MessageBox.Show("" + er); }
编辑:似乎如果我像下面显示的那样拆分查询,它会起作用,我真的不明白为什么会这样。
try
{
//BESTEL,[PLAN],LEV,ADRES,TAAL
SqlCommand getlist = new SqlCommand("select BESTEL,[PLAN],ADRES from BESW where BEST=@best", Connectie.connMEVO);
getlist.Parameters.Add("@best", SqlDbType.VarChar).Value = data.corrigeerbestnr;
DRorder = getlist.ExecuteReader();
while (DRorder.Read())
{
dateTimePicker1.Value = Convert.ToDateTime(DRorder["BESTEL"]);
dateTimePicker2.Value = Convert.ToDateTime(DRorder["PLAN"]);
comboBox2.Text = DRorder["ADRES"].ToString();
}
SqlCommand getlist2 = new SqlCommand("select LEV from BESW where BEST=@best", Connectie.connMEVO);
getlist2.Parameters.Add("@best", SqlDbType.VarChar).Value = data.corrigeerbestnr;
DRorder = getlist2.ExecuteReader();
while (DRorder.Read())
{
comboBox1.Text = DRorder["LEV"].ToString();
}
SqlCommand getlist3 = new SqlCommand("select TAAL from BESW where BEST=@best", Connectie.connMEVO);
getlist3.Parameters.Add("@best", SqlDbType.VarChar).Value = data.corrigeerbestnr;
DRorder = getlist3.ExecuteReader();
while (DRorder.Read())
{
textBox8.Text = DRorder["TAAL"].ToString();
}
}
catch (Exception er) { MessageBox.Show("" + er); }