使用c#从sql数据库中检索图像,但是出错,请给出解决方案 [英] Retrieve image from the sql database using c#, but giving an error, please give the solution
本文介绍了使用c#从sql数据库中检索图像,但是出错,请给出解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void cmbEmpId_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection ca = new SqlConnection();
ca.ConnectionString = c1.pass();
ca.Open();
string str = "Select * From EmpDetails where Emp_Id='" + cmbEmpId.Text.ToString() + "'";
SqlCommand cam = new SqlCommand(str, ca);
SqlDataReader sdr = cam.ExecuteReader();
while (sdr.Read())
{
txtAddress.Text = Convert.ToString(sdr[5]);
txtCity.Text = Convert.ToString(sdr[6]);
cmbCountry.Text = Convert.ToString(sdr[7]);
txtContactNo.Text = Convert.ToString(sdr[8]);
txtEMailId.Text = Convert.ToString(sdr[9]);
cmbQualification.Text = Convert.ToString(sdr[10]);
cmbDesignation.Text = Convert.ToString(sdr[12]);
cmbDepartment.Text = Convert.ToString(sdr[13]);
byte[] img_arr1 = (byte[])sdr["photo"];
MemoryStream ms = new MemoryStream();
ms.Position = 0;
ms.Write(img_arr1, 0, img_arr1.Length);
pictureBox1.Image = Image.FromStream(ms); <--- Giving Error "Parameter is not valid".
}
}
推荐答案
你好朋友,你的解决方案在这里
Hello Friends, Your Solution is Here
private void cmbEmpId_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection ca = new SqlConnection();
ca.ConnectionString = c1.pass();
ca.Open();
string str = "Select * From EmpDetails where Emp_Id='" + cmbEmpId.Text.ToString() + "'";
SqlCommand cam = new SqlCommand(str, ca);
SqlDataReader sdr = cam.ExecuteReader();
while (sdr.Read())
{
txtAddress.Text = Convert.ToString(sdr[5]);
txtCity.Text = Convert.ToString(sdr[6]);
cmbCountry.Text = Convert.ToString(sdr[7]);
txtContactNo.Text = Convert.ToString(sdr[8]);
txtEMailId.Text = Convert.ToString(sdr[9]);
cmbQualification.Text = Convert.ToString(sdr[10]);
cmbDesignation.Text = Convert.ToString(sdr[12]);
cmbDepartment.Text = Convert.ToString(sdr[13]);
//byte[] img_arr1 = (byte[])sdr["photo"];
//MemoryStream ms = new MemoryStream();
//ms.Position = 0;
//ms.Write(img_arr1, 0, img_arr1.Length);
//pictureBox1.Image = Image.FromStream(ms); <--- Giving Error "Parameter is not valid".
MemoryStream ms = new MemoryStream((byte[])sdr["photo"]);
pictureBox1.Image = Image.FromStream(ms);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Refresh();
}
}
设置filemode属性以创建内存流
set the filemode property to create of memory stream
这篇关于使用c#从sql数据库中检索图像,但是出错,请给出解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文