First of all, I don't recommend storing images in the database, especially in a web project. Because file systems can provide static file access and get many benefits such as performance. Storing images in the database has certain limits.

But if you must store images into SQL Server, here's how to do it. 

SQL


Image files are in fact, binary data. SQL Server provides us the image type for storing images. So we can create a table like this, with a column named Pic as image type.

CREATE TABLE [dbo].[ImgTest]
(
    [Id]   [uniqueidentifier] NOT NULL,
    [Pic]  [image] NOT NULL,
    CONSTRAINT [PK_ImgTest] PRIMARY KEY CLUSTERED([Id] ASC)WITH (
        PAD_INDEX = OFF,
        STATISTICS_NORECOMPUTE = OFF,
        IGNORE_DUP_KEY = OFF,
        ALLOW_ROW_LOCKS = ON,
        ALLOW_PAGE_LOCKS = ON
    ) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Then, map this table with Entity Framework. Finally, you will get an entity type like this:

namespace EFImgTest.Models
{
    using System;
    using System.Collections.Generic;

    public partial class ImgTest
    {
        public System.Guid Id { get; set; }
        public byte[] Pic { get; set; }
    }
}

As we can see, Pic column is mapped to byte[] array. Now we need to deal with converting image data to byte[] and backward.

Upload Image to Database


When you submit an image file in MVC3, you need to set Form's enctype to multipart/form-data:

@using (Html.BeginForm("Create", "ImgTest", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ImgTest</legend>
        <input type="file" name="imgFile" id="imgFile" />
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

Add an extra parameter HttpPostedFileBase imgFile in the Action to receive the uploaded image. Then, read it into binary data using HttpPostedFile.InputStream.Read().

[HttpPost]
public ActionResult Create(ImgTest imgtest, HttpPostedFileBase imgFile)
{
    if (ModelState.IsValid)
    {
        if (imgFile.ContentLength > 0)
        {
            var imgByte = new byte[imgFile.ContentLength];
            imgFile.InputStream.Read(imgByte, 0, imgFile.ContentLength);
            imgtest.Id = Guid.NewGuid();
            imgtest.Pic = imgByte;
            db.ImgTest.Add(imgtest);
            db.SaveChanges();
        }
        return RedirectToAction("Index"); 
    }
    return View(imgtest);
}

After you uploaded the image, it's data in the database looks like this: 

If you would like to insert image data into SQL Server by T-SQL, here's how to do it:

INSERT INTO ImgTest
  (
    Id,
    Pic
  )
SELECT NEWID(),
       BulkColumn
FROM   OPENROWSET(BULK 'D:\Edi\tempfolder\img-5b394358-da2e-4a0a-ab32-a740123a44img-1b702a0a-e147-4e42-b579-7a19bf68b5cd.png', SINGLE_BLOB) AS Pic

Read Image from byte[]


Razor engine doesn't provide built-in methods for displaying images via byte[]. So we need to write a custom Action for converting byte[] into FileContentResult:

public ActionResult GetImage(Guid id)
{
    var imgByte = db.ImgTest.Find(id).Pic;
    return new FileContentResult(imgByte, "image/jpeg");
}

Use it in a view:

<img src="@Url.Content("~/ImgTest/GetImage/" + item.Id)" />