我有一个"指定演员阵容无效"错误.C#中的Windows表单应用程序.我正在尝试从表中检索值.该值是smallint或数字(我试过两个字段,都给我相同的错误),我尝试将它存储在一个int变量中.
这里是来源:
using (SqlDataReader rdr = cmd.ExecuteReader()) //"select * from table where fieldname = " + value
{
 while (rdr.Read())
 {
  int number = (int)rdr["quantity"]; // error is here
jas*_*son 19
rdr["quantity"]将是一个盒装的东西.如果它不是int那么你就不能直接将它拆箱到int(这是你想要做的),因为你必须首先将它拆箱到适当的类型(比方说short).但这太麻烦了,为了清楚起见,你最好说
Convert.ToInt32(rdr["quantity"]);
小智 5
  if(rdr["quantity"].GetType() != typeof(int))
    throw new InvalidOperationException(
      "quantity is a " + rdr["quantity"].GetType());
  int number = (int)rdr["quantity"]; // error is here
| 归档时间: | 
 | 
| 查看次数: | 55468 次 | 
| 最近记录: |