ADO.Net updating a BLOB
-
I have been pawing all over the ADO.Net references and still can't see an obvious way to update a BLOB field. Any clues? TIA
Look at ".Net Data Access Architecture Guide" on MSDN: Writing BLOB Data to the Database The following code shows how to use ADO.NET to write binary data obtained from a file to an image field in SQL Server.
public void StorePicture( string filename ) {
// Read the file into a byte array
FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read );
byte[] imageData = new Byte[fs.Length];
fs.Read( imageData, 0, (int)fs.Length );
fs.Close();SqlConnection conn = new SqlConnection("Connection String");
SqlCommand cmd = new SqlCommand("StorePicture", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@filename", filename );
cmd.Parameters["@filename"].Direction = ParameterDirection.Input;
cmd.Parameters.Add("@blobdata", SqlDbType.Image);
cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input;
// Store the byte array within the image field
cmd.Parameters["@blobdata"].Value = imageData;
try {
conn.Open();
cmd.ExecuteNonQuery();
}
catch {
throw;
}
finally {
conn.Close();
}
}