gpt4 book ai didi

c# - 转换varchar值时转换失败

转载 作者:行者123 更新时间:2023-12-03 08:04:31 25 4
gpt4 key购买 nike

当我尝试运行此命令时,它给我以下错误消息:

Conversion failed when converting the varchar value 'category_id' to data type int.



这是我的SQL和参数代码,我认为应该可以,但不能。
mycmd.CommandText="SELECT * FROM categories WHERE @db_property = @property_id"; 

// This contains a string "category_id", which is correct.
mycmd.Parameters.Add("@db_property", SqlDbType.VarChar).Value=db_property_field;

// This contains an Int, referring to the category_id in database. As of now, this is 1
mycmd.Parameters.Add("@property_id", SqlDbType.Int).Value=property_id;

在检查完这段代码后,我通过阅读器运行它,这就是上面显示错误消息的地方。一直问老师,和我类的优秀学生一样,没人能找到问题所在的线索。

最佳答案

您不应该添加字段名称作为参数。尝试更改您的脚本以包括实际的字段ID:

mycmd.CommandText = "SELECT * FROM categories WHERE category_id = @property_id";
mycmd.Parameters.Add("@property_id", SqlDbType.Int).Value = property_id;

关于c# - 转换varchar值时转换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15200060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com