Insertion d'images dans BDD sql Compact
Bonjour, je développe une petite application Windows Mobile. le but est de prendre des photos et les enregistrer dans la BDD.
alors j'ai crée un table PHOTO avec deux colonnes, ID int(4) auto-incrément et une autre de type image.
Voila mon code :
Code:
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;
} |
j'obtiens une exception à l'ExecuteNonQuery() et l'insert ne ce fait pas.
Citation:
Le nombre des colonnes dans la requête et dans la table ne correspond pas. [ Number of columns in query = 1, Number of columns in table = 2 ]
c'est la premier fois que je fais des enregistrement d'images et je suis un peu perdu...
Merci d'avance