操作数类型冲突:nvarchar与图像不兼容 [英] Operand type clash: nvarchar is incompatible with image
问题描述
我正在使用SQL Server 2008存储过程以
这种语法创建一条新记录:
I'm using a SQL Server 2008 stored procedure to create a new record with this syntax:
cmd.Parameters.Add("@photo", DBNull.Value)
cmd.ExecuteNonQuery()
但结果是:
Operand type clash: nvarchar is incompatible with image
照片不是唯一的参数,但是唯一的一个图像,我没有传递
a nvarchar但是空值,我是遗失了什么?
Photo is not the only parameter but is the only image one, I am not passing a nvarchar but a null value, am I missing something?
推荐答案
如果你传入 DBNull.Value
作为值,ADO.NET无法弄清楚参数应该是什么类型。如果指定字符串或整数值,则可以从提供的值派生SQL参数的类型 - 但是应该将 DBNull.Value
转换为什么类型?
If you pass in DBNull.Value
as the value, ADO.NET can't figure out what type the parameter should be. If you specify a string, or an integer value, the type of the SQL parameter can be derived from the value provided - but what type should DBNull.Value
be turned into??
传入NULL值时,需要自己明确指定 SqlDbType
:
When passing in a NULL value, you need to specify that SqlDbType
yourself, explicitly:
Dim photoParam As New SqlParameter("@photo", SqlDbType.Image)
photoParam.Value = DBNull.Value
cmd.Parameters.Add(photoParam)
cmd.ExecuteNonQuery()
那个应该工作,我希望!
That should work, I hope!
更新: C#中的相同内容是:
Update: same thing in C# would be:
SqlParameter photoParam = new SqlParameter("@photo", SqlDbType.Image);
photoParam.Value = DBNull.Value;
cmd.Parameters.Add(photoParam);
cmd.ExecuteNonQuery();
有一个很棒的,免费的,非常有用的 VB.NET-to-C#转换器 - 使用它!
There's a great, free, very useful VB.NET-to-C# converter out there - use it!
这篇关于操作数类型冲突:nvarchar与图像不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!