我需要一些帮助。我用过数据中继器来显示 数据库中的值,所有列都显示转发器上文本框中的值,但图像除外。无法将字节转换回图像 这是我保存到数据库的代码
private void btnSave_Click(object sender, EventArgs e)
{
byte[] imageBt = null;
FileStream fstream = new FileStream(this.txtImgPath.Text,FileMode.Open,FileAccess.Read);
BinaryReader Br = new BinaryReader(fstream);
imageBt = Br.ReadBytes((int)fstream.Length);
// byte[] pic = stream.ToArray();
try
{
conDB.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = conDB;
command.CommandText = "insert into abaanaCC (CCSpn_CODE,CCFname,CCLname,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian,CCContact,CCImage)" +
" values ('" + spn_codetxt.Text + "','" + txtfname.Text + "','" + lnametxt.Text + "','" + mnametxt.Text + "','" + DOBDTPicker1.Text + "','" + gendercomboBox.Text + "','" + schtxt.Text + "','" + classcomboBox.Text + "','" + villatxt.Text + "','" + siblingscombobx.Text + "','" + guardiantxt.Text + "','" + contacttxt.Text + "',@IMG) ";
command.Parameters.Add(new OleDbParameter("@IMG",imageBt));
//command.Parameters.AddWithValue("@IMG",pic);
command.ExecuteNonQuery();
MessageBox.Show("Record Saved");
}
catch (Exception ex)
{
MessageBox.Show("Unable to save" + ex);
}
conDB.Close();
}
然后是数据中继器
private void Update_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'abaanaDataSet.abaanaCC' table. You can move, or remove it, as needed.
this.abaanaCCTableAdapter.Fill(this.abaanaDataSet.abaanaCC);
int c = this.abaanaDataSet.abaanaCC.Rows.Count;
if (c > 0) ;
{
byte[] mydata = new byte[0];
mydata = (byte[])(this.abaanaDataSet.abaanaCC.Rows[c-1]["CCImage"]);
MemoryStream stream = new MemoryStream(mydata);
cCImagePictureBox.Image = Image.FromStream(stream);
}
}
答案 0 :(得分:0)
确保您实际从数据集中获取有效的图像数据。这应该有效:
private void Form3_Load(object sender, EventArgs e)
{
this.productPhotoTableAdapter.Fill(this.adventureWorks2014DataSet.ProductPhoto);
byte[] mydata = (byte[])this.adventureWorks2014DataSet.ProductPhoto[100]["LargePhoto"];
MemoryStream stream = new MemoryStream(mydata);
pictureBox1.Image = Image.FromStream(stream);
}