检索并保存图像 [英] Retrieve and save an image

查看:72
本文介绍了检索并保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MSSQL数据库中有一个名为photo的Image类型列。数据库的名称也是照片(我知道这很糟糕)。

我想得到这个图像并将其存储回其他记录中。

这是我的代码:

b.com.CommandText =从照片中选择照片WHERE模型= @ model;

b.com.Parameters.Add(@ model,txt_model.Text .Trim());

if(b.con.State == ConnectionState.Closed)

b.con.Open();

b.dr = b.com.ExecuteReader();

if(b.dr.HasRows)

{

b.dr.阅读();

b.com.Parameters.Add(@ j,b.dr.GetValue(0));

b.dr.Close() ;

}



当我尝试再次插入此图像时,ExecuteScalar()抛出此异常。



I have an Image type column in MSSQL database named photo. The name of database is also photo (I know thats bad).
I want to get this image and store it back in some other record.
Here is my code:
b.com.CommandText = "SELECT photo from photo WHERE model=@model";
b.com.Parameters.Add("@model", txt_model.Text.Trim());
if (b.con.State == ConnectionState.Closed)
b.con.Open();
b.dr = b.com.ExecuteReader();
if (b.dr.HasRows)
{
b.dr.Read();
b.com.Parameters.Add("@j", b.dr.GetValue(0));
b.dr.Close();
}

Here is my exception thrown by ExecuteScalar() when I tried to insert this image again.

Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.





我尝试过:



这是我的代码:

b.com.CommandText =从照片中选择照片WHERE model = @ model;

b.com.Parameters.Add(@ model,txt_model.Text.Trim());

if(b.con.State == ConnectionState.Closed)

b.con.Open();

b.dr = b.com.ExecuteReader();

if(b.dr.HasRows)

{

b.dr.Read();

b.com.Parameters.Add(@ j,b.dr.GetValue(0)) ;

b.dr.Close();

}



这是ExecuteScalar抛出的异常(当我试图再次插入此图像时。





What I have tried:

Here is my code:
b.com.CommandText = "SELECT photo from photo WHERE model=@model";
b.com.Parameters.Add("@model", txt_model.Text.Trim());
if (b.con.State == ConnectionState.Closed)
b.con.Open();
b.dr = b.com.ExecuteReader();
if (b.dr.HasRows)
{
b.dr.Read();
b.com.Parameters.Add("@j", b.dr.GetValue(0));
b.dr.Close();
}

Here is my exception thrown by ExecuteScalar() when I tried to insert this image again.

Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.

推荐答案

或许,你没有显示的查询会抛出错误,试图返回一个图像,因为它是标量值 - 此时你会得到那个错误。 />
如果没有围绕ExecuteScalar的实际代码和实际的SQL,这是不可能的,但是他们可能会尝试做类似的事情:

Probably, the query you don't show that throws the error is trying to return an image as it's scalar value - at which point you would get that error.
Without the actual code around the ExecuteScalar and the actual SQL it's impossible to tell, but they chances are that you are trying to do something like:
int i = (int) cmd.ExecuteScalar();

你是哪个人不能用字节数组。

看看这个:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ],它给出了我使用的代码。

Which you can't do with a byte array.
Have a look at this: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] which gives the code I use.


这篇关于检索并保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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