469,575 Members | 1,658 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

SQL UPDATE record with Image field

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();
}
}
--
Tinius
Nov 16 '05 #1
2 20735
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Tinius" <Ti****@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
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();
}
}
--
Tinius

Nov 16 '05 #2
Hi Ignacio

Perfect

Thanks

Tinius

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by oldandgrey | last post: by
4 posts views Thread by RDRaider | last post: by
1 post views Thread by Daniel | last post: by
6 posts views Thread by Matt | last post: by
4 posts views Thread by Laurahn | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.