【发布时间】:2020-01-23 18:20:35
【问题描述】:
下面的例子展示了选择随机的 combobobx 项目后会发生什么。 Sql 查询已成功运行,但是我收到错误“指定的强制转换不是有效的 c#”。
组合框 listaPrzedmiotow 以字符串形式存储学校科目列表。 组合框 listaIndeksów 应该存储学生索引列表。在 SQL Server 数据库中 student indices 列接受整数,所以我不知道为什么 .GetInt64(i) 方法对此无效。
private void listaPrzedmiotow_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(listaPrzedmiotow.SelectedItem.ToString());
SqlCommand sqlCommand = new SqlCommand("select studenci.NUMER_INDEKSU from dbo.oceny inner join studenci on studenci.numer_indeksu = oceny.numer_indeksu inner join dbo.przedmioty on przedmioty.przedmiot_id = oceny.przedmiot_id where przedmioty.NAZWA_PRZEDMIOTU = '" + listaPrzedmiotow.SelectedItem.ToString() + "'", sqlConnection);
SqlDataReader indexesList = sqlCommand.ExecuteReader();
while (indexesList.Read())
{
for (byte i = 0; i < indexesList.FieldCount; i++)
{
listaIndeksów.Items.Add(indexesList.GetInt64(i));
}
}
indexesList.Close();
indexesList.Dispose();
}
【问题讨论】: