c#如何从数据库中检索图像 [英] How to retrieve image from database in c#
本文介绍了c#如何从数据库中检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个错误显示我有此代码的无效参数...谁能告诉我怎么了?我应该得到分配给 clientID 的图像.
an error shows saying that I have invalid parameters for this code... can anyone tell me whats wrong? Im supposed to get the image assigned to the clientID.
private void button1_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection(mycon);
MySqlCommand cmd = new MySqlCommand("SELECT clientImage FROM client WHERE clientID='" + label2.Text + "'", conn);
conn.Open();
MySqlDataReader myReader = null;
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
byte[] imgg = (byte[])(myReader["clientImage"]);
if (imgg == null)
{
pictureBox1.Image = null;
}
else
{
MemoryStream mstream = new MemoryStream(imgg);
pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
}
}
conn.Close();
}
推荐答案
此代码有效.MySql Wamp.
this code works. MySql Wamp.
using System.IO;
string appPath = Path.GetDirectoryName(Application.Executable) + @"/student Images/";
string imagename;
//put this code below to load form.
getimage();
if(File.Exist(appPath + imagename)
{
PictureBox1.Image = Image.FromFile(appPath + imagename);
}
else
{
PictureBox1.Image = Properties.Resources.Image_notfound;
}
private void getimage()
{
MySqlConnection connect = new MySqlConnection(con);
MySqlCommand cmd = new MySqlCommand("SELECT PictureName as 'pic' FROM userprofile where ID='" + datagrid.CurrentRow.Cells["ID"].ToString() + "'");
cmd.CommandType = CommandType.Text;
cmd.Connection = connect;
connect.Open();
Try
{
MySqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
imagename = dr.GetString("pic");
}
dr.Close();
}
catch(Exception ee)
{
Console.WriteLine(ee.ToString());
}
finally
{
connect.Close();
}
这篇关于c#如何从数据库中检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文