By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

SQL UPDATE record with Image field

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.