从 SQL Server 数据库检索图像
我有一个问题,当我从内存流中的数据库检索图像时,它给出错误参数无效
。请帮助我解决这个问题。
代码:
private void button3_Click(object sender, EventArgs e)
{
string strcon = "Data Source=PINKAL-PC; initial catalog=testing; integrated security=SSPI;";
SqlConnection sqlcon = new SqlConnection(strcon);
sqlcon.Open();
string strquery = "select * from testimg";
SqlDataAdapter da = new SqlDataAdapter(strquery,sqlcon);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
byte[] barrImg = (byte[])dt.Rows[7]["image"];
MemoryStream mstream = new MemoryStream(barrImg);
pictureBox2.Image = Image.FromStream(mstream);
}
I have a question that when I retrieve image from database from memory stream it gives an error Parameter is not valid
. Please help me out from this problem.
Code:
private void button3_Click(object sender, EventArgs e)
{
string strcon = "Data Source=PINKAL-PC; initial catalog=testing; integrated security=SSPI;";
SqlConnection sqlcon = new SqlConnection(strcon);
sqlcon.Open();
string strquery = "select * from testimg";
SqlDataAdapter da = new SqlDataAdapter(strquery,sqlcon);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
byte[] barrImg = (byte[])dt.Rows[7]["image"];
MemoryStream mstream = new MemoryStream(barrImg);
pictureBox2.Image = Image.FromStream(mstream);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定问题的实际根本原因是什么 - 我的猜测是数据库表
testimg
中没有名为image
的列,但是您尝试阅读该专栏以获取您的图片。但我一般会为您的代码推荐以下几点:
Not sure what the actual root cause of your problem is - my guess would be that there is no column by the name of
image
in your database tabletestimg
, but you're trying to read that column to get your picture.But here are a few things I'd recommend for your code in general:
如果您的错误与将图像从数据库转换为流有关,请尝试像这样修改您的代码:
If your error is concerned with converting the image from database into stream, Just try to modify your code like this::