C# 强制转换无效,但它以前是
我不知道发生了什么事。这段代码昨天运行得很好,但今天当我尝试运行它时,在标记的位置收到“指定的强制转换无效”错误。查询返回1到25之间的整数。在Access数据库中,字段的数据类型为“number”,字段大小为“long integer” 关于如何纠正这个问题有什么线索吗C# 强制转换无效,但它以前是,c#,casting,datareader,C#,Casting,Datareader,我不知道发生了什么事。这段代码昨天运行得很好,但今天当我尝试运行它时,在标记的位置收到“指定的强制转换无效”错误。查询返回1到25之间的整数。在Access数据库中,字段的数据类型为“number”,字段大小为“long integer” 关于如何纠正这个问题有什么线索吗 static void PopulateClientList() { Console.WriteLine("Populating Client List..."); Console.W
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();
}
}
}
static void PopulateClientList()
{
Console.WriteLine(“填充客户端列表…”);
控制台写入线(“\r”);
OLEDB连接连接=新的OLEDB连接(STRACCESCONNABS);
string query=“从订单中选择不同的客户机,其中状态=‘保留’;”;
OleDbCommand命令=新的OleDbCommand(查询,连接);
conn.Open();
OleDbDataAdapter=新的OleDbDataAdapter(查询,连接);
//OleDbDataReader=command.ExecuteReader();
DataTable dt=新的DataTable();
尝试
{
//if(reader.HasRows)
//{
//while(reader.Read())
// {
//clientlist.Add(reader.GetInt32(0));//此处发生强制转换无效错误
//}
// }
适配器填充(dt);
如果(dt.Rows.Count>=1)
{
对于(int i=0;i
您的问题是,可能已将超过最大值Int32
的值添加到您的表中。在此之前,您的代码不会引发异常
该字段的数据类型为“number”,字段大小为“long integer”
那你为什么在这里把它读成Int32
clientlist.Add(reader.GetInt32(0)); //cast not valid error happens here
将其更改为:
clientlist.Add(reader.GetInt64(0));
根据clientlist
是什么,您还必须将其类型更改为接受Int64
s
在Access数据库中,字段的数据类型为“number”,带有
字段大小='长整数'
您没有使用长时间:
clientlist.Add(reader.GetInt32(0)); //cast not valid error happens here -> Use GetInt64
我想有人把DB字段从int>long改了?我刚刚注意到了这个问题。但我不知道这是怎么发生的。在数据库中,当我运行查询时,第一行是空的。当读卡器读到它并试图将它添加到列表中时,它不会这样做,因为它是一个空值 您在代码中更改了什么?您在数据库中更改了什么?引发异常时读取器[0]的值是多少?Convert.ToInt32(reader.GetValue(0))?