简体   繁体   中英

A default varbinary(max) value for image

I have a web application which has a user to insert images. I am finding a way to set a default varbinary(MAX) value in database (SQL Server 2008) . Maybe something like Facebook which has a default profile picture once you sign up. I am able to upload images to the database and display it out. I able to generate those image into a PDF file (if the image column is NOT NULL) else I get error decoding my image, so I need a default varbinary(MAX) image to represent that the column of image is not inserted. Please help me out really in need of it for my project.

If your are looking at storing files/images I would recomend looking at FILESTREAM Overview

Much of the data that is created every day is unstructured data, such as text documents, images, and videos. This unstructured data is often stored outside the database, separate from its structured data. This separation can cause data management complexities. Or, if the data is associated with structured storage, the file streaming capabilities and performance can be limited.

FILESTREAM integrates the SQL Server Database Engine with an NTFS file system by storing varbinary(max) binary large object (BLOB) data as files on the file system. Transact-SQL statements can insert, update, query, search, and back up FILESTREAM data. Win32 file system interfaces provide streaming access to the data.

FILESTREAM uses the NT system cache for caching file data. This helps reduce any effect that FILESTREAM data might have on Database Engine performance. The SQL Server buffer pool is not used; therefore, this memory is available for query processing.

You can insert an image, or any kind of file, into a SQL row using code like this:

/*get blob from file, store in variable*/
declare @blob as varbinary(max)
set @blob = (
    Select BulkColumn from Openrowset
    ( Bulk 'c:\temp\MyImage.jpg', Single_Blob) as tt)

/* insert blob into row */
insert into dbo.MyImageTable (Name, Blob) 
values('SomeName',@blob)    

Using this, you can store a single default image on a table then just link it to the rows that need a default image using a simple join like this:

 select * from MyData join MyImageTable on MyImageTable.Name='SomeName'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM