你好,我有以下代碼:的ExecuteReader,提升價值,以名單,指數超出範圍
SqlCeCommand commandArbeitstage= new SqlCeCommand("select count(IDStundensatz) as Gesamt from tblstunden Where IDPersonal = @IDPersonal Group by datepart(month, Datum) Order By datepart(month, Datum)", verbindung);
commandArbeitstage.Parameters.Add("IDPersonal", SqlDbType.Int);
commandArbeitstage.Parameters["@IDPersonal"].Value = IDPersonal;
SqlCeDataReader readerArbeitstage = commandArbeitstage.ExecuteReader();
List<Int32> Arbeitstage = new List<Int32>();
while (readerArbeitstage.Read())
{
Arbeitstage.Add(readerArbeitstage.GetInt32(0));
}
Arbeitstage.GetRange
textBox53.Text = Arbeitstage[0].ToString();
textBox60.Text = Arbeitstage[1].ToString();
textBox68.Text = Arbeitstage[2].ToString();
查詢由datepart來計算工作日一張桌子和訂購。所以我有一列[Gesamt]和12行。我想將工作天數分配給12個文本框。 1月至3月我已經完成了上述工作。
如果我添加的四月份另一行代碼,
textBox74.Text = Arbeitstage[3].ToString();
我得到了超出範圍的異常。我認爲問題的發生是因爲4月份沒有記錄,因此List Arbeitstage中的索引[4]不存在。所以我想爲textBox74.Text指定一個零。
有人有想法嗎?
非常感謝提前!
或代替相當多餘「的if-else」序列,就可以分配長度12的歸零陣列,然後通過枚舉這是從數據庫返回的值更新此陣列和最後將存儲在數組中的值分配給各個文本框,而不檢查任何條件。 – nullop
@nullop非常好的一點,我更新了我的答案。 – Szymon
我試着用我的第一個和第二個查詢,但數組被填充的行數:1 2 3而不是在列天的值:即3 1 2. – user3204119