如何从数据库中检索位图图像?

问题描述 投票:0回答:1

我正在执行搜索操作并将图像从数据库中检索到pictureEdit控件中。我将图像存储为varchar(4000),并且图像以System.Drawing.Bitmap的格式存储。搜索时出现错误

无法将类型为'System.String'的对象转换为类型为'System.Byte []'

我的代码:

private void btnSearch_Click(object sender, EventArgs e)
{
        byte[] getImg = new byte[0];

        SqlDataAdapter dataAdapter = new SqlDataAdapter("select sid,sname,fathername,contactno,address,sphoto from Student where sclass='" + lukupClass.Text + "' and ssection='" + lukupSection.Text + "' and sname='" + textBox1.Text + "'", sqlConnection);

        DataTable dt = new DataTable();
        dataAdapter.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            txtID.Text = dt.Rows[0]["sid"].ToString();
            txtStudentName.Text = dt.Rows[0]["sname"].ToString();
            txtFatherName.Text = dt.Rows[0]["fathername"].ToString();
            txtContactNo.Text = dt.Rows[0]["contactno"].ToString();
            memoAddress.Text = dt.Rows[0]["address"].ToString();
            getImg = ((byte[])dt.Rows[0]["sphoto"]);
            byte[] imgData = getImg;
            MemoryStream stream = new MemoryStream(imgData);
            picStudent.Image = Image.FromStream(new MemoryStream(getImg));
        }
        else
        {
            MessageBox.Show("No Data Found with this Name.");
        }
}
c# database bitmapimage
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.