Im using C# to insert an image to Mysql database. The length of byte array is 51115. However, When I checked in database, the length of the field IMage (Long Blob) is only 13 bytes. What wrong is it? Please help me!
private void saveBtn_Click(object sender, EventArgs e)
{
try
{
Image img = imgBox.Image;
byte[] arr;
ImageConverter converter = new ImageConverter();
arr = (byte[])converter.ConvertTo(img, typeof(byte[]));
MessageBox.Show(arr.Length.ToString()); // 51115
string connectionString = "server=localhost;user id=root;password=xrayadmin;database=xraydatabase";
string query = "INSERT INTO imagedatabase(IDPatient,Image,DiagnosisDoctor,DiagnosisDate) VALUES('" + Convert.ToInt32(labPatID.Text) + "','" +
arr + "','" + labDocUser.Text + "','" + DateTime.Now.ToString("dd / MM / yyyy") + "')";
MySqlConnection MysqlConnection = new MySqlConnection(connectionString);
MySqlCommand MysqlCmd = new MySqlCommand(query, MysqlConnection);
MySqlDataReader DataReader;
MysqlConnection.Open();
DataReader = MysqlCmd.ExecuteReader();
MessageBox.Show("Save Data");
while (DataReader.Read())
{
}
MysqlConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
I recomend you to use parameters. Like this
string query = @"INSERT INTO imagedatabase(IDPatient,Image,DiagnosisDoctor,DiagnosisDate) VALUES(@IDPatient,@Image,@DiagnosisDoctor,@DiagnosisDate)"
MysqlCmd.Parameters.AddWithValue("@Image", arr);