1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
private void SqlInsertValuesRequest()
{
try
{
SqlCeConnection SqlCnx = new SqlCeConnection(@"Data Source=\Program Files\MemoPhoto5\Database.sdf");
string sSQL = "INSERT INTO PHOTO VALUES (?)";
SqlCeCommand SqlCommand = new SqlCeCommand(sSQL, SqlCnx);
byte[] photo = GetPhoto(textBox1.Text.Trim());
SqlCeParameter ParamImage = new SqlCeParameter("IMAGE", SqlDbType.Image, photo.Length);
ParamImage.Value = photo;
SqlCommand.Parameters.Add(ParamImage);
SqlCnx.Open();
SqlCommand.Prepare();
SqlCommand.ExecuteNonQuery();
if (SqlCnx.State == ConnectionState.Open)
{
SqlCnx.Close();
}
MessageBox.Show("Requête SQL effectuée avec succès", "OK", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
catch (SqlCeException Ex)
{
MessageBox.Show("Erreur SQL inattendue.\n" + Ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
catch (Exception Ex)
{
MessageBox.Show("Erreur inattendue.\n" + Ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
private byte[] GetPhoto(string filePath)
{
FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
byte[] photo = reader.ReadBytes((int)stream.Length);
reader.Close();
stream.Close();
return photo;
} |
Partager