Operand type clash: nvarchar is incompatible with image

岁酱吖の 提交于 2019-12-01 14:36:31

问题


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()

but the result is a:

Operand type clash: nvarchar is incompatible with image 

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?


回答1:


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??

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!

Update: same thing in C# would be:

SqlParameter photoParam = new SqlParameter("@photo", SqlDbType.Image);
photoParam.Value = DBNull.Value;
cmd.Parameters.Add(photoParam);

cmd.ExecuteNonQuery();

There's a great, free, very useful VB.NET-to-C# converter out there - use it!




回答2:


 Dim photo_NULL As New SqlTypes.SqlBytes
.Pararameters.AddWithValue("@photo", IIf(IsNothing(Photo), photo_NULL, Photo))
.CommandType = CommandType.StoredProcedure
F_return = (.ExecuteNonQuery > 0)


来源:https://stackoverflow.com/questions/2420708/operand-type-clash-nvarchar-is-incompatible-with-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!