Home > C# > Add and Retrieve Image into database use LINQ to SQL

Add and Retrieve Image into database use LINQ to SQL


First we will create a table call tblImage(id,images)

and then create a class datacontext and add tblImage to this datacontext class

– Create storeprocedure

CREATE PROCEDURE IMAGE_INSERT
@IMAGE IMAGE
AS
INSERT INTO TBLIMAGE VALUES(@IMAGE)

– Code to add Image to database

byte[] data;
Image im = Image.FromFile(@”d:\keyboard.bmp”);
MemoryStream msread = new MemoryStream();
im.Save(msread, System.Drawing.Imaging.ImageFormat.Bmp);
data = msread.ToArray();
System.Data.Linq.Binary bi = new System.Data.Linq.Binary(data);
DBTEST.IMAGE_INSERT(bi);

– Code to Retrieve Image from database

var image = (from t in DBTEST.tblImages
orderby t.id descending
select t.hinhanh).FirstOrDefault();

byte[] imagedata = image.ToArray();
MemoryStream ms = new MemoryStream(imagedata);
Image i = Image.FromStream(ms);

Ok, this is all my know about how to add or retrieve images to database use LINQ to SQL.

Hope this article will useful for you!.

Thanks.

Categories: C#
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: