我必须从SQL DB中读取图像 [英] I have to read Image from SQL DB
问题描述
我已经将图像保存在SQL DB中,但是现在我必须读取图像并将其显示到DATA GRID VIEW中.但是我不能阅读图片.
Hi I have saved Image in SQL DB but now i have to read Image and have to display it into DATA GRID VIEW. but i can't read the Image.
我正在发布代码,请帮忙
I am posting code kindly help me out
ArticleImage是byte [].
ArticleImage is byte[].
BindingList<Article> articleList = new BindingList<Article>();
Article article;
StringBuilder qry = new StringBuilder();
qry.Append("select * from vw_GetAllArticles");
try
{
using (SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.QST_Order_Management_CON_STRING, CommandType.Text, qry.ToString(), null))
{
if(dr.HasRows == false)
{
return null;
}
int counter = 0;
while( dr.Read())
{
article = new Article();
article.SrNo = counter+= 1;
article.ArticleID = dr["ArticleID"] != DBNull.Value ? Convert.ToInt32(dr["ArticleID"]) : -1;
article.ArticleNo = dr["ArticleNo"] != DBNull.Value ? Convert.ToString(dr["ArticleNo"]) : string.Empty;
article.ArticleCode = dr["ArticleCode"] != DBNull.Value ? Convert.ToString(dr["ArticleCode"]) : string.Empty;
// Here is the problem
article.ArticleImage = dr["ArticleImage"] != DBNull.Image ? (dr["ArticleImage"]) :
articleList.Add(article);
}
return articleList;
}
}
catch (Exception ex)
{
LogHelper.AppendToFile("Stack Trace...\"" +ex.ToString()+"\"");
throw ex;
}
推荐答案
该代码甚至不应该编译. DBNull没有名为Image的方法或属性.而且您缺少三元声明的最后一个操作数.不幸的是,我没有去猜测你的问题是因为你没有告诉 我.您所写的只是看不懂图片.
That code shouldn't even compile. DBNull does not have a method or property called Image. And you're missing the last operand of your ternary statement. Unfortunately, I'm left to guess what your problem is because you didn't tell me. All you wrote was that you couldn't read the image.
我知道您需要做的三件事:
Three things I know you need to do:
- 修复它,使其显示为DBNull.Value
- 将数据读取器中的值转换为字节数组
- 在其中添加"else"三元声明的一部分
article.ArticleImage = dr["ArticleImage"] != DBNull.Value
? (byte[])dr["ArticleImage"]
: null);
这篇关于我必须从SQL DB中读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!