Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 操作数类型冲突:int与uniqueidentifier不兼容_C#_Sql Server 2005 - Fatal编程技术网

C# 操作数类型冲突:int与uniqueidentifier不兼容

C# 操作数类型冲突:int与uniqueidentifier不兼容,c#,sql-server-2005,C#,Sql Server 2005,我收到一个SQLException“操作数类型冲突:int与uniqueidentifier不兼容” 当我试图从C#代码执行下面的存储过程时 C代码如下所示 cmd = new SqlCommand("sp_Get_Allfields_Account_Public", con); cmd.CommandType = CommandType.StoredProcedure; // Add Input Parameters

我收到一个SQLException“操作数类型冲突:int与uniqueidentifier不兼容” 当我试图从C#代码执行下面的存储过程时

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();
表定义

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

这似乎有点多余,但您确定列
[Account\u Public].[acc\u num]
的数据类型实际上是
唯一标识符,而不是
int

请尝试以下方法:

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;
使用相同的参数。你也会犯同样的错误吗


另外,我强烈建议您在所有char参数上指定显式大小。它对于输入参数并不重要,但是对于输出参数非常重要。

这是因为您正在调用
AddWithValue
,然后将该值作为枚举传递(被解释为
int


使用不同的方法(可能只是
Add
)。

是ExecuteOnQuery上的错误还是AddWithValue上的错误?例外情况是ExecuteOnQuerys是一个例外,永远不要在存储过程名称前加
sp
-这些应该为系统过程保留。看,我想就是这样。刚刚发现WithValue部分。尝试了Add方法,但这次我得到了异常字符串[3]:Size属性的大小无效为0。@Nadeem您需要为varchar输出变量指定一个大小,否则它默认为0,并且您不能有nvarchar(0)。
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;
cmd.Parameters.AddWithValue("@acc_num", SqlDbType.UniqueIdentifier);