1. 程式人生 > >在sqlserver中操作image、varbinary

在sqlserver中操作image、varbinary

今天想把圖片存到資料庫中的image,varchar,nvarchar,varbinary欄位, 終於成功了.儲存到資料庫的程式碼如下:

  HttpPostedFile UpFile = UP_FILE.PostedFile;  //HttpPostedFile物件,用於讀取圖象檔案屬性
        int FileLength = UpFile.ContentLength;     //記錄檔案長度
        try
        {
            if (FileLength == 0)
            {   //檔案長度為零時
                txtMessage.Text = "<b>請你選擇你要上傳的檔案</b>";
            }
            else
            {
                Byte[] FileByteArray = new Byte[FileLength];   //圖象檔案臨時儲存Byte陣列
                Stream StreamObject = UpFile.InputStream;      //建立資料流對像
                //讀取圖象檔案資料,FileByteArray為資料儲存體,0為資料指標位置、FileLnegth為資料長度
                StreamObject.Read(FileByteArray, 0, FileLength);
                //建立SQL Server連結
                SqlConnection Con = new SqlConnection("Data Source=YFXIANGGX;Initial Catalog=jsmstc_teach;User ID=sa;Pwd=1;");
                //type 上傳檔案型別,length上傳檔案長度,info image,info1 NVarChar,info2 VarChar,info3 VarBinary
                String SqlCmd = "INSERT INTO BigField (type,length,info,info1,info2,info3) VALUES (@type,@length,@info,@info1,@info2,@info3)";
                SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
                CmdObj.Parameters.Add("@type", SqlDbType.Char, 50).Value = UpFile.ContentType;
                CmdObj.Parameters.Add("@length", SqlDbType.Int).Value = UpFile.ContentLength;
                CmdObj.Parameters.Add("@info", SqlDbType.Image).Value = FileByteArray;
                CmdObj.Parameters.Add("@info1", SqlDbType.NVarChar).Value = FileByteArray.ToString();
                CmdObj.Parameters.Add("@info2", SqlDbType.VarChar).Value = FileByteArray.ToString();
                CmdObj.Parameters.Add("@info3", SqlDbType.VarBinary, FileLength).Value = FileByteArray;

                Con.Open();
                CmdObj.ExecuteNonQuery();
                Con.Close();
                txtMessage.Text = "<p><b>OK!你已經成功上傳你的圖片</b>";//提示上傳成功
            }
        }
        catch (Exception ex)
        {
            txtMessage.Text = ex.Message.ToString();
        }

頁面程式碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="saveall.aspx.cs" Inherits="saveall" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>無標題頁</title>
</head>
<body>
<h1>測試資料庫對於大欄位()的存取</h1>
    <form id="form1" runat="server">
    <div>
        <table style="width: 488px">
            <tr>
                <td>
                    上傳圖片(選擇你要上傳的圖片)&lt;/</td>
                <td>
                    <asp:FileUpload ID="UP_FILE" runat="server" /></td>
                <td>
                </td>
            </tr>
            <tr>
                <td style="height: 40px">
                    檔案說明(新增上傳圖片說明,如:作者、出處)</td>
                <td style="height: 40px">
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="uploadImage" /></td>
                <td>
                    <asp:Label ID="txtMessage" runat="server" Text="Label"></asp:Label></td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

從資料庫中讀出時遇到了問題,首先是如何將二進位制的資料讀出後顯示到image上,其次是對於儲存成varchar,nvarchar欄位的值無法取出來了.都沒有解決.讀出的資料只能通過response輸出到當前頁面,無法直接在image控制元件上輸出.儲存為image,varbinary欄位的值取出來的程式碼如下:
        string ImgID = "1";  //ImgID為圖片ID
        //建立資料庫連結
        SqlConnection Con = new SqlConnection("Data Source=YFXIANGGX;Initial Catalog=jsmstc_teach;User ID=sa;Pwd=1;");
        String SqlCmd = "SELECT * FROM BigField WHERE id = @id";
        SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
        CmdObj.Parameters.Add("@id", SqlDbType.Int).Value = ImgID;
        Con.Open();
        SqlDataReader SqlReader = CmdObj.ExecuteReader();
        SqlReader.Read();
        //Response.ContentType = (string)SqlReader["ImageContentType"];//設定輸出檔案型別
        Response.ContentType = SqlReader["type"].ToString();//設定輸出檔案型別
        //輸出圖象檔案二進位制數制       
        Response.OutputStream.Write((byte[])SqlReader["info"], 0, (int)SqlReader["length"]);//或者是SqlReader["info3"],
        Response.End();

        Con.Close();