Type用于在数据库中保存图像的数据类型

时间:2012-03-10 19:22:52

标签: asp.net telerik

我需要使用SQL插入本地图像。

我这样做:

CREATE TABLE [dbo].[Table_1](
      [SelectedImages] [image] NOT NULL,
      [Path] [ntext] NOT NULL
)

to add an image, do this:

INSERT INTO [testing].[dbo].[Table_1]
           ([SelectedImages]
           ,[Path])
     VALUES
           ('D:\desktop\05022006\free_chart1.gif' ,'D:\desktop\05022006\free_chart1.gif' )

问题是我需要在之后检索图像......

我使用Telerik控件显示在数据库中存储为二进制数据的图像 我不确定“图像”类型是否正确,因为它不起作用......

                  <telerik:RadBinaryImage runat="server" ID="RadBinaryImage1" DataValue='<%#Eval("Photo") %>'
                        AutoAdjustImageControlSize="false" Width="90px" Height="110px" ToolTip='<%#Eval("ContactName", "Photo of {0}") %>'
                        AlternateText='<%#Eval("ContactName", "Photo of {0}") %>' />

您使用哪种数据类型来存储图像? 提前致谢

2 个答案:

答案 0 :(得分:2)

您可以使用图像类型将图像存储在数据库中,就像您在问题中定义的那样。但是,您需要先将图像转换为字节数组。

string path = "D:\\desktop\\05022006\\free_chart1.gif"
using (System.IO.FileStream fstream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
  // Create a buffer to hold the stream of bytes
  byte[] buffer = new byte[fstream.Length];
  // Read the bytes from this stream and put it into the image buffer
  fstream.Read(buffer, 0, (int)fstream.Length);
  fstream.Close();
  //now you can insert buffer into the database
}

如果要从数据库中检索图像,则必须将其从字节[]转换回来。

if (byteArray != null)
{
  if (byteArray.Length > 0)
  {
    // Create a new MemoryStream and write all the information from the byte array into the stream
    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray, true))
    {
      ms.Write(byteArray, 0, byteArray.Length);

      // Use the MemoryStream to create the new BitMap object
      Bitmap photo = new Bitmap(ms);

      ms.Close();

      //now you can use photo
    }
  }
}

答案 1 :(得分:1)

  

您使用哪种数据类型来存储图像?

  1. 这取决于情况&amp;你的电话。

  2. 有时只将图像的URL存储在VARCHAR字段中 足够'C:/Data/UserName/uploads/myGuid+filename.jpg'&amp;实际文件存在于磁盘上(使用此方法很难进行事务管理,但适用于小的要求)

  3. 当事务管理是最高优先级时,您可以将它们存储在VARBINARY(max)字段Storing images in SQL Server
  4. 中的数据库中
  5. SQL Server文件流也是最新SQLServer的一个选项,文件存储在由数据库SQL Server filestream Overview管理的文件系统上