I am trying to store an image in my SQL Server database, what datatype should I use?
In the below code aspx.cs
, I am trying to read all the bytes from the request inputstream and store it in the database, but the byte[]
array is not updated properly in table. Am I missing something?
protected void Page_Load(object sender, EventArgs e)
{
Request.InputStream.Position = 0;
byte[] Contents = new byte[Request.InputStream.Length];
Request.InputStream.Read(Contents, 0, (int)Request.InputStream.Length);
con.Open();
try
{
string query = "update tblImageUpload set " + IMAGE_ID + " = @imageBytes where Image_ID='" + CID + "'";
int i = 0;
using (cmd = new SqlCommand(query, con))
{
cmd.Parameters.Add("@imageBytes", SqlDbType.VarBinary, Contents.Length).Value = Contents;
i = cmd.ExecuteNonQuery();
}
Response.Write("Upload Query = " + query);
Response.Write("Upload Code = " + i);
} catch (Exception ex) {
Response.Write("Upload Code=" + ex);
}
You can use VARBINARY
yes. You're probably best off going with VARBINARY(MAX)
to store them.
You can use it like this:
cmd.Parameters.Add("@imageBytes", SqlDbType.VarBinaryMax);
cmd.Parameters["@imageBytes"].Value = Contents;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.