且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

图像未保存在数据库中

更新时间:2021-09-19 22:38:08

您创建一个新的 SqlCommand 对象名为 cmd btnsave_Click 方法中,作为局部变量。但是,当您再调用 conv_photo 来保存图像时,代码中引用了一个不同的 cmd 对象在代码中的其他地方声明。您应该将 cmd 对象作为参数传递给 conv_photo ,这样您就可以确定它正在寻找正确的对象。
You create a new SqlCommand object named cmd inside your btnsave_Click method, as a local variable. However, when you then call conv_photo to save the image the code there is referring to a different cmd object that has been declared somewhere else in your code. You should pass the cmd object as a parameter to conv_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 called cmd to which you are adding the image as a parameter in your conv_photo method. But...in your btnsave_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.