从MySQL数据库检索blob图片c#
我使用此代码来检索我的图片,它正常工作与一个简单的表,只包含blob,但是当我试图适应它为我的表用户containt(cin,nom, prenom ....,图像)异常指示从MySQL数据库检索blob图片c#
“Paramétre非有效”(不是一个有效参数)
int bufferSize = 1000;
try
{
string SQL = "Select image from user ";
MySqlCommand cmd = new MySqlCommand(SQL, db.Connection);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "image");
int c = ds.Tables["image"].Rows.Count;
db.CloseConnection();
if (c > 0)
{
Byte[] byteBLOBData = new Byte[bufferSize];
byteBLOBData = (Byte[])(ds.Tables["image"].Rows[c - 1]["image"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image = Image.FromStream(stmBLOBData);
MessageBox.Show("bien chargée");
}
}
catch (Exception ex)
{
MessageBox.Show("Connection Error!\n" + ex.Message, "Error Message",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
试试这个...
DataTable userTable;
DataTable ds;
int cin;
string nom;
string prenom;
Byte[] ImageByte;
userTable = ds;
if (userTable == null)
return false;
else
{
if (userTable.Rows.Count > 0)
{
foreach (DataRow userRow in userTable.Rows)
{
cin = Convert.ToInt32(userRow["cin"]);
nom = userRow["nom"].ToString();
prenom = userRow["prenom"].ToString();
ImageByte = (Byte[])(userRow["image"]);
}
}
}
if (ImageByte != null)
{
// You need to convert it in bitmap to display the imgage
pictureBox1.Image = ByteToImage(ImageByte);
pictureBox1.Refresh();
}
public static Bitmap ByteToImage(byte[] blob)
{
MemoryStream mStream = new MemoryStream();
byte[] pData = blob;
mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(mStream, false);
mStream.Dispose();
return bm;
}
谢谢,但我只需要一个blob图片到picturebox –
请检查我更新的答案。 您需要将您的字节数组转换为位图。请检查** ByteToImage(byte [] byteArray)**方法 – Riz
它仍然不工作! :( –
byteBLOBData = ((Byte[])ds.Tables["image"].Rows[c - 1]["image"]);
这应该解决它。
你可以在你的代码中添加一些解释,而不是只发布答案吗?试着帮助人们明确哪些部分是错的,为什么错了,以及为什么你的解决方案是他们需要应用的。 – LordWilmore
在哪一行出现异常? – Gusman
我认为你不必填写数据集,但直接使用SqlDataReader –
如果我删除捕获我没有得到错误,没有结果... –