Retriving image data from sdf database to ASP.NET razor RRS feed

  • Question

  • User1450159794 posted


    I need to save  image data to sdf Database & retrive image data from sdf Database with ASP.NET Web pages razor.

    I have some code as bellow.

    File : index.cshtml

    int id = 5;
    var db = Database.Open("InventoryDB");
    var sql = "Select * From InventoryTBL Where itemID = @0";
    var file = db.QuerySingle(sql, id);
    if (file.mimeType.StartsWith("image/"))
       Response.AddHeader("content-disposition", "inline; filename=" + file.fileName);
       Response.AddHeader("content-disposition", "attachment; filename=" + file.fileName);
    Response.ContentType = file.MimeType;
    var db2 = Database.Open("InventoryDB");
    var sql2 = "Select * From InventoryTBL";
    <!DOCTYPE html>

            @foreach (var row in db2.Query(sql2))
                        //string mypath = "/images/" + @row.fileName;
                        //<td><img src="@mypath" alt="img" /></td>
                        //Response.ContentType = @row.MimeType;
    <a href="InsertProducts.cshtml">Add New </a>
    <a href="EditProducts.cshtml">EDIT </a>
    <a href="DeleteProduct.cshtml">Delete </a>

    In this code with bellow 2 lines
    Response.ContentType = file.MimeType;

    It displayes the batabase retrived image(one image) in the full page.
    But what I want to display is the list of images in the database with other information(eg: name ,quantity etc... )

    can some one help please ??
    Sunday, May 14, 2017 4:03 AM

All replies

  • User-2057865890 posted

    Hi deelliiee,

    You could save image path in Database and display the image like this

    <img src="@mypath" alt="img" />

    Best Regards,


    Wednesday, May 17, 2017 6:54 AM
  • User1450159794 posted

    Thank you Chris Zhao.

    I also thought of that. That way data is not protected.Anybody can go to the folder where images are and can manipulate them.

    But some how I found this code that saves images in binary data into the database.

    what I want is to retrieve data from Database & display the list of items with image in the page .

    Can someone have an idea how to do it ?

    Wednesday, May 17, 2017 10:36 AM
  • User1967761114 posted

    Hi deelliiee,

    According to your description, you can read the image bytes from database, then convert the bytes to base64 string which can direct show on image.

    See the following code, it might help you:

        Layout = null;
        //read bytes from database
        var bytes = (byte[])DBAccess.SqlHelper.ExecuteScalar(DBAccess.SqlHelper.GetConnSting(), System.Data.CommandType.Text, "SELECT top 1 [image] FROM [Test]");
        //convert bytes to base64 string
        var imageData = Convert.ToBase64String(bytes);
        <!-- set image data -->
        <img src="data:image/png;base64,@(imageData)" />

    If you have any other questions, please feel free to contact me any time.

    Best Regards


    Tuesday, May 30, 2017 8:15 AM