无法更新表格。错误是:不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。 [英] can not update table . error is : Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

查看:135
本文介绍了无法更新表格。错误是:不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新表格。错误是:不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。



I am trying to update table . the error is :Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

//convert image to byte
            MemoryStream ms = new MemoryStream();
            picperson.Image.Save(ms, picperson.Image.RawFormat);
            byte [] arrImage = ms.GetBuffer();
            ms.Close();







objconnection.Open();
                objcommand.CommandText =
                    "UPDATE profile SET  picture='"+arrImage+
                    "' WHERE id=" + txtid.Text;




插入sql命令中的
我没有这个错误但是当我使用update命令时出现了这个错误?

我想在图像字段类型为varbinary的数据库表中更改图像.I图像字段类型不是图像类型。 arrImage转换为byte然后保存到varbinary类型的图片字段。

我说插入sql命令没有问题,但更新错误。



in insert sql command i have not this error but when I use update command taken this error?
I want change image in database table where picture field type is varbinary.I picture field type is not image type. arrImage convert to byte then save to picture field with varbinary type.
I say where I have not problem with Insert sql command but update error.

推荐答案

那不行。不是INSERT,也不是UPDATE。

您可能认为它确实如此,但是......看看这个:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 它解释了发生了什么的基础知识。



现在想一想:ToString为一个字节数组返回什么?

答案:System.Byte []

当你连接字符串形成一个SQL命令时,你结束了up with:

That won''t work. Not as an INSERT, or as an UPDATE.
You might think it does, but...have a look at this: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it explains the basics of what is happening.

Now think: What does ToString return for a byte array?
Answer: "System.Byte[]"
And when you concatenate strings to form an SQL command, you end up with:
UPDATE profile SET  picture='System.Byte[]' WHERE id=888



因此,请使用参数化查询:


So, instead, use parametrized queries:

objcommand.CommandText = "UPDATE profile SET picture=@IM WHERE id=@ID";
objcommand.Parameters.AddWithValue("@ID", txtid.Text);
objcommand.Parameters.AddWithValue("@IM", arrImage);

这也有一个好处,即用户不能通过键入文本框并导致SQL注入攻击来破坏或破坏数据库!

This also has the advantage that your users can''t damage or destroy your database by typing in the textbox and causing an SQL injection attack!


http://stackoverflow.com/questions/11411854/implicit-conversion-from -data型为nvarchar到varbinarymax-IS-不被允许 [ ^ ]


这篇关于无法更新表格。错误是:不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆