【问题标题】:{"Operand type clash: int is incompatible with text"}{“操作数类型冲突:int 与文本不兼容”}
【发布时间】:2023-03-26 20:05:01
【问题描述】:

每当我运行此查询时,我都会收到此错误。 感谢您的帮助。

{"操作数类型冲突:int 与文本不兼容"}

这是我的代码:

SqlCommand cmd1 = cons.CreateCommand();
                cmd1.CommandType = CommandType.Text;
                cmd1.CommandText = "update coffeeshop set Quantity=convert(varchar(MAX),Quantity)-" + t1.Text;

                cmd1.ExecuteNonQuery();

如果我拿走 convert(varchar(Max),如下面的代码所示:

 SqlCommand cmd1 = cons.CreateCommand();
        cmd1.CommandType = CommandType.Text;
        cmd1.CommandText = "update coffeeshop set Quantity=Quantity-" + t1.Text;
        cmd1.ExecuteNonQuery();

然后我得到这个错误:

{"操作数类型冲突:文本与 int 不兼容"}

【问题讨论】:

  • 你表中Quantity的类型是什么?另外,t1.Text 的值是多少?
  • 数量类型是我的数据库表中的文本,t1.text 的值取决于用户,因为这是一个空文本框,供用户插入一个数量,然后在我的查询中减去它可以看到。

标签: c# asp.net


【解决方案1】:

假设数量为varchar

cmd1.CommandText = "update coffeeshop set Quantity= Convert(int,Quantity)-" + t1.Text;

text 试试这个:

cmd1.CommandText = "update coffeeshop set quantity= Convert(varchar(max) , Convert(INT, Convert(varchar(max),quantity))- "+ t1.Text + ")";

【讨论】:

  • {"不允许从数据类型 text 到 int 的显式转换。"} 这是我得到的错误
  • @khans 查看我的编辑。如果不完全符合我所写的内容,应该对您有所帮助。
猜你喜欢
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 2012-06-03
  • 2019-10-31
  • 2016-07-09
  • 1970-01-01
  • 2015-08-09
  • 1970-01-01
相关资源
最近更新 更多