CS0029:无法隐式转换类型' string'到' int'

时间:2016-06-02 04:00:37

标签: c#

当我试图跑但是错误

时,我遇到了这个错误
  

描述:编译服务此请求所需的资源时发生错误。请查看以下特定错误详细信息并相应地修改源代码。

     

编译器错误消息:CS0029:无法隐式转换类型' string'到' int'

 cmd.Parameters.AddWithValue("@pDateOfDelivery", po.dateOfDelivery);
                cmd.Parameters.AddWithValue("@pStatus", po.status);
                cmd.Parameters.AddWithValue("@pDeliveryTiming", po.deliveryTiming);
                cmd.Parameters.AddWithValue("@pCoID", po.coID);

                try
                {
                    conn.Open();
                    int iResult = cmd.ExecuteNonQuery();
                    if (iResult > 0)
                    {
                        result = "You have successfully updated the DeliveryOrder!";
                    }
                    else
                    {
                        result = "You have encountered an error. Please try again.";
                    }
                }
                catch (Exception)
                {
                    result = "You have encountered an error. Please try again";
                }
                finally
                {
                    cmd.Dispose();
                    conn.Close();
                }
            }
        }
        return result;
    }

3 个答案:

答案 0 :(得分:0)

您的一个参数是期望int类型,但您正在传递string类型。

尝试下面在传递之前将字符串转换为int。

cmd.Parameters.AddWithValue("@pCoID", Int32.Parse(po.coID));

答案 1 :(得分:0)

使用Convert.ToInt32方法将字符串变量转换为整数

cmd.Parameters.AddWithValue("@pCoID",Convert.ToInt32(po.coID));

答案 2 :(得分:0)

您应该使用cmd.Parameters.Add()这样您就可以指定期望的数据类型;

如果您使用OdbcCommands,则参数的数据类型将为getattr(),如果您使用的是SqlCommand,则为OdbcType。简而言之,数据类型将取决于命令类型。这是SqlDbType的示例。

 cmd.Parameters.Add("@", SqlDbType.Int).Value = po.coID;

确保po.coID是整数类型。否则你需要将其转换为整数;我想建议SqlDbType进行此次转化