更新时间:2021-09-19 22:38:08
您创建一个新的SqlCommand
对象名为cmd
在btnsave_Click
方法中,作为局部变量。但是,当您再调用conv_photo
来保存图像时,代码中引用了一个不同的cmd
对象在代码中的其他地方声明。您应该将cmd
对象作为参数传递给conv_photo
,这样您就可以确定它正在寻找正确的对象。
You create a newSqlCommand
object namedcmd
inside yourbtnsave_Click
method, as a local variable. However, when you then callconv_photo
to save the image the code there is referring to a differentcmd
object that has been declared somewhere else in your code. You should pass thecmd
object as a parameter toconv_photo
so you are sure it is addressing the correct object.
这很简单:你有一个类级别的SqlCommand对象,名为cmd
,你要将图像作为参数添加到conv_photo
方法。但是......在你的btnsave_Click
方法中,你创建了一个同名的新局部变量:
It's pretty simple: you have a class level SqlCommand object calledcmd
to which you are adding the image as a parameter in yourconv_photo
method. But...in yourbtnsave_Click
method you create a new local variable of the same name:
SqlCommand cmd = new SqlCommand(cmdtext, con);
你用它来做INSERT操作。
因为他们不喜欢两者都使用相同的SqlCommand实例,@ this参数被添加到错误的SqlCommand并且SQL因此抱怨。
我?我将删除类级别版本,并将相应的SqlCommand对象传递给 conv_photo
方法,以确保始终将其添加到正确的实例。
你的转换不需要那么复杂:
Which you use to do teh INSERT operation.
Because they don't both use the same SqlCommand instance, the @photo parameter is added to the wrong SqlCommand and SQL complains as a result.
Me? I'd delete the class level version, and pass the appropriate SqlCommand object to the conv_photo
method to ensure you always add it to the correct instance.
And your conversion doesn't need to be so complex:
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] data = ms.ToArray();
将会这样做。
will do it.