我正在尝试将图像存储在我的 SQL Server 数据库中,我应该使用什么数据类型?
在下面code aspx.cs
,我试图从请求输入流中读取所有字节并将其存储在数据库中,但byte[]
数组未在表中正确更新。我错过了什么吗?
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);
}