簡體   English   中英

我可以使用varbinary類型在SQL Server數據庫中存儲圖像嗎?

[英]Can I use varbinary type to store image in SQL Server database?

我試圖在我的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);
        }

您可以使用VARBINARY yes。 你可能最好使用VARBINARY(MAX)存儲它們。

你可以像這樣使用它:

cmd.Parameters.Add("@imageBytes", SqlDbType.VarBinaryMax);
cmd.Parameters["@imageBytes"].Value = Contents;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM