Retrieving and storing an image in SQL Server

Tags:
SQL Server
Visual Basic
Visual Basic 2008
How can I retrieve and store an image in SQL Server using Visual Basic 2008?

Answer Wiki

Thanks. We'll let you know when a new response is added.

If you are using SQL 2000 or below you will want to use the data type of Image. If you have using SQL 2005 or above then you will want to use the data type of VARBINARY(MAX).

Your best bet will be to load up the image into a .NET variable and pass it to a stored procedure.

The stored procedure can then handle the writing of the data to the table.

CREATE PROCEDURE InsertImage
   @Image VARBINARY(MAX)
AS
INSERT INTO YourTable
(Image)
VALUES
(@Image)
GO

As for retrieving the image, a single select will return the data to you. Write the data back out to a file (if your images can be in different formats you’ll want to store the file extension in the table, unless you want to try and pull it from the binary data it self).

Now, do keep in mind that it’s recommended that you not keep the actual images within the database. It’s better to store them on a file server somewhere and only keep the path to the file in the database. This will shrink your database file size, and improve response time as the system grows.

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following