我不确定发生了什么。这段代码昨天运行良好,但今天当我尝试运行它时,我在标记的位置收到“指定的强制转换无效”错误。该查询返回介于 1 和 25 之间的整数。在 Access 数据库中,该字段的数据类型为“数字”,字段大小 =“长整数”。
有关如何纠正此问题的任何线索?
static void PopulateClientList()
{
Console.WriteLine("Populating Client List...");
Console.WriteLine("\r");
OleDbConnection conn = new OleDbConnection(strAccessConnABS);
string query = "SELECT DISTINCT Client FROM ORDERS WHERE Status = 'On Hold';";
OleDbCommand command = new OleDbCommand(query, conn);
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
//OleDbDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable();
try
{
//if (reader.HasRows)
//{
// while (reader.Read())
// {
//clientlist.Add(reader.GetInt32(0)); //cast not valid error happens here
//}
// }
adapter.Fill(dt);
if (dt.Rows.Count >= 1)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][0].ToString() != "")
clientlist.Add((int)dt.Rows[i][0]);
}
}
}
catch (OleDbException ex)
{
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}