SQL UPDATE record with Image field

G

Guest

I am trying to update a field of type IMAGE.
I am able to do this by deleting the record and then INSERTing a new record
as shown below in the commented section.

But I really want to do this with an UPDATE command as shown below in the
un-commented code. I assume this is possible.

The image is passed as a byte array called 'content'. I then want to update
the column STREAM with the new 'CONTENT'.

I don't know the correct SQL syntax for this UPDATE command.


protected static void StoreImage(byte[] content, int iID)
{
OleDbConnection con = ConnectionOledb;
con.Open();
try
{
// update record with image
OleDbCommand update = new OleDbCommand("UPDATE Images SET [Stream] = " +
content + " WHERE ID = " + iID, con);
update.ExecuteNonQuery();

//Long winded way. Delete whole record, then replace
// OleDbCommand delete = new OleDbCommand("DELETE * FROM Images WHERE ID "
+ iID, con);
// delete.ExecuteNonQuery();
//
// OleDbCommand insert = new OleDbCommand("INSERT into Images ([stream],
ID) values (@image, @ID)", con);
// OleDbParameter imageParameter = insert.Parameters.Add("@image",
OleDbType.Binary);
// imageParameter.Value = content;
// imageParameter.Size = content.Length;
// OleDbParameter IDParam = insert.Parameters.Add("@ID", OleDbType.Integer);
// IDParam.Value = iID;
// insert.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You should be using parameters for the update, in the same way you are
doing it for the insert

OleDbCommand update = new OleDbCommand("UPDATE Images SET [Stream] = @image
WHERE ID = " + iID, con);

OleDbParameter imageParameter = insert.Parameters.Add("@image",
OleDbType.Binary);
imageParameter.Value = content;
imageParameter.Size = content.Length;

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top