这是我的代码:
try
{
con = new MySqlConnection(conname);
con.Open();
//
string query = " Select monday_img_slot1 from faculty_attend_record where idfaculty='"+id+"'";
MySqlCommand cmd = new MySqlCommand(query, con);
MySqlDataReader da = cmd.ExecuteReader();
if(da.HasRows)
{
da.Read();
byte[] img = (byte[])da[0];
// error is in the line below
MemoryStream ms = new MemoryStream(img);
captureImage.Image = Image.FromStream(ms);
}
con.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
答案 0 :(得分:0)
您将图像路径存储在数据库中(列monday_img_slot1),而不是实际图像。 需要先将图像转换为Byte,然后将其存储在数据库中。
下面是一个演示如何在数据库中上传图像的示例(表名=“ images”,“数据”列为longblob,“ id”为int,“ name”为字符串类型)
注意:您需要使用准备好的语句来避免mysql注入。
MySqlConnection con=null;
try
{
string myConnectionString = "server=localhost;database=test;uid=root;pwd=root;";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Image files | *.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
con = new MySqlConnection(myConnectionString);
string FileName = openFileDialog1.FileName;
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] ImageData = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
string CmdString = "INSERT INTO images(id, name, data) VALUES(@id, @name, @data)";
MySqlCommand cmd = new MySqlCommand(CmdString, con);
cmd.Parameters.Add("@id", MySqlDbType.Int32);
cmd.Parameters.Add("@name", MySqlDbType.VarChar, 45);
cmd.Parameters.Add("@data", MySqlDbType.LongBlob);
cmd.Parameters["@id"].Value = 5;
cmd.Parameters["@name"].Value = textBox1.Text;
cmd.Parameters["@data"].Value = ImageData;
con.Open();
int RowsAffected = cmd.ExecuteNonQuery();
if (RowsAffected > 0)
{
MessageBox.Show("Image saved sucessfully!");
}
}
else
{
MessageBox.Show("Incomplete data!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con!=null && con.State == ConnectionState.Open)
{
con.Close();
}
}
在您使用update命令之前的情况下,请将图像转换为字节:
MySqlConnection con=null;
try
{
con = new MySqlConnection(conname);
string captureimg = path;
FileStream fs = new FileStream(captureimg, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] ImageData = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
string CmdString = "Update faculty_attend_record set monday_slot1=@monday_slot1,monday_img_slot1=@monday_img_slot1,monday_class_slot1=@monday_class_slot1,monday_room_slot1=@monday_room_slot1 where idfaculty=@idfaculty";
MySqlCommand cmd = new MySqlCommand(CmdString, con);
cmd.Parameters.Add("@monday_slot1", MySqlDbType.VarChar, 50);
cmd.Parameters.Add("@monday_img_slot1", MySqlDbType.LongBlob);
cmd.Parameters.Add("@monday_class_slot1", MySqlDbType.VarChar, 50);
cmd.Parameters.Add("@monday_room_slot1", MySqlDbType.VarChar, 50);
cmd.Parameters.Add("@idfaculty", MySqlDbType.VarChar, 50);
cmd.Parameters["@monday_slot1"].Value = "present";
cmd.Parameters["@monday_img_slot1"].Value = ImageData;
cmd.Parameters["@monday_class_slot1"].Value = classname1.Text;
cmd.Parameters["@monday_room_slot1"].Value = RoomNo1.Text;
cmd.Parameters["@idfaculty"].Value = idfaculty;
con.Open();
int RowsAffected = cmd.ExecuteNonQuery();
if (RowsAffected > 0)
{
MessageBox.Show("Image saved sucessfully!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con!=null && con.State == ConnectionState.Open)
{
con.Close();
}
}
注意:由于我不完全了解表中存在的列的数据类型,因此我假设它们都是字符串和一个longbolb,因此您可以根据需要进行修改。