C# 在C中从MySQL检索LONGBLOB
我想从MySQL数据库中检索LONGBLOB,但我不知道如何检索。我搜索了interwebz,没有找到任何真正有用的东西。检索LONGBLOB时,我希望将其保存为图像 这是我已经尝试过的:C# 在C中从MySQL检索LONGBLOB,c#,mysql,blob,C#,Mysql,Blob,我想从MySQL数据库中检索LONGBLOB,但我不知道如何检索。我搜索了interwebz,没有找到任何真正有用的东西。检索LONGBLOB时,我希望将其保存为图像 这是我已经尝试过的: int bufferSize = 100; byte[] bin = new byte[bufferSize]; long retval = 0; long startIndex = 0; MemoryStream ms = n
int bufferSize = 100;
byte[] bin = new byte[bufferSize];
long retval = 0;
long startIndex = 0;
MemoryStream ms = null;
Image image = null;
MySqlCommand command = new MySqlCommand("select * from image where uid = @uid", Connection.Connect());
command.Parameters.AddWithValue("@uid", "2");
MySqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
retval = reader.GetBytes(reader.GetOrdinal("logo"), startIndex, bin, 0, bufferSize);
}
ms = new MemoryStream(bin);
image = Image.FromStream(ms);
提前谢谢。我不确定,但我想你可以试试这个:
stefan实际上,我是在一个项目中做这件事的 public Bitmap loadImage(int imgID) { MySqlDataReader myData; MySqlCommand cmd = new MySqlCommand(); string SQL; byte[] rawData; MemoryStream ms; UInt32 FileSize; Bitmap outImage; SQL = "SELECT ImageName, ImageSize, Image FROM Images WHERE ImageID ="; SQL += imgID.ToString(); try { cmd.Connection = connection; cmd.CommandText = SQL; myData = cmd.ExecuteReader(); if (!myData.HasRows) throw new Exception("There are no blobs to save"); myData.Read(); FileSize = myData.GetUInt32(myData.GetOrdinal("ImageSize")); rawData = new byte[FileSize]; myData.GetBytes(myData.GetOrdinal("Image"), 0, rawData, 0, (Int32)FileSize); ms = new MemoryStream(rawData); outImage = new Bitmap(ms); ms.Close(); ms.Dispose(); myData.Close(); myData.Dispose(); cmd.Dispose(); return outImage; } catch (MySqlException ex) { MessageBox.Show(ex.Message); return null; } } 希望这有帮助。另外,请原谅任何不好的编码实践,这是前一段时间写的 谢谢
Tom获取错误:image=image.fromstreams;参数无效。