简体   繁体   English

操作数类型碰撞:int与uniqueidentifier不兼容

[英]Operand type clash: int is incompatible with uniqueidentifier

I am getting an SQLException "Operand type clash: int is incompatible with uniqueidentifier" when I am trying to execute the below stored procedure from C# code. 当我尝试从C#代码执行下面的存储过程时,我得到一个SQLException“操作数类型冲突:int与uniqueidentifier不兼容”。

create proc sp_Get_Allfields_Account_Public
@username varchar(20),
@password varchar(20),
@acc_num UniqueIdentifier out,
@cust_name varchar(20) out,
@balance float out
as
select @acc_num=acc_num,@cust_name=cust_name,@balance=balance from Account_Public where username=@username and password=@password

C# code is as follows C#代码如下

cmd = new SqlCommand("sp_Get_Allfields_Account_Public", con);
               cmd.CommandType = CommandType.StoredProcedure;

               // Add Input Parameters
               cmd.Parameters.AddWithValue("@username", username);
               cmd.Parameters.AddWithValue("@password", password);   

               // Add output parameters
               cmd.Parameters.AddWithValue("@acc_num", SqlDbType.UniqueIdentifier);
               cmd.Parameters["@acc_num"].Direction = ParameterDirection.Output;

               cmd.Parameters.AddWithValue("@cust_name", SqlDbType.VarChar);
               cmd.Parameters["@cust_name"].Direction = ParameterDirection.Output;

               cmd.Parameters.AddWithValue("@balance", SqlDbType.Float);
               cmd.Parameters["@balance"].Direction = ParameterDirection.Output;

               cmd.ExecuteNonQuery();

Table definition 表定义

create table Account_Public
(
acc_num uniqueidentifier,
cust_name varchar(20),
username varchar(20),
password varchar(20),
balance float
)

It's because you're calling AddWithValue , then passing the value as the enum (which is interpreted as an int ). 这是因为你正在调用AddWithValue ,然后将值作为枚举传递(它被解释为int )。

cmd.Parameters.AddWithValue("@acc_num", SqlDbType.UniqueIdentifier); 

Use a different method (probably just Add ). 使用不同的方法(可能只是Add )。

This may seem a bit redundant, but are you sure that the data type of column [Account_Public].[acc_num] is actually uniqueidentifier and not int ? 这可能看起来有点多余,但您确定列[Account_Public].[acc_num]的数据类型实际上是uniqueidentifier而不是int吗?

Try this instead: 试试这个:

cmd = new SqlCommand("select @acc_num=acc_num,@cust_name=cust_name,@balance=balance from Account_Public where username=@username and password=@password", con);
cmd.CommandType = CommandType.Text;

with the same parameters. 具有相同的参数。 Do you get the same error? 你得到同样的错误吗?

Also, I strongly recommend that you specify explicit sizes on all the char parameters. 另外,我强烈建议您在所有char参数上指定显式大小。 It's not so important for the input parameters, but it's very important for the output ones. 输入参数并不重要,但对输出参数非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM