Results 1 to 4 of 4

Thread: Inserting and retrieving images from Database

  1. #1
    Join Date
    Nov 2009
    Posts
    39

    Inserting and retrieving images from Database

    Can you help me to load and insert the images into database through programming in ASP.NET and I am using SQLServer as a backend.
    I have a problem while loading and retrieving an image from database.
    I have loaded an image in the database as of type Image. I have retrieved the data from database and passed to stream. How can I load this to a picture box or to a report viewer?

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Inserting image in Database

    Before inserting the image into database,we will go through steps-

    PersonImage, an object and one of the HTMLInputFile control. Before doing anything we need to get the size of that particular image that we are going to insert using -

    intImageSize = PersonImage.PostedFile.ContentLength

    Now,we will ready to retrieve image but before that the most important thing is that we need to get the Image Stream using-

    ImageStream = PersonImage.PostedFile.InputStream
    we are having an array of bytes ,imagecontent that will hold the image content and it would be done using the method Read(),it takes 3 parameters-

    Target Location,Starting position,Number of bytes that requires to read.

    intStatus = ImageStream.Read(ImageContent, 0, intImageSize)

    After all of reading and getting the image from client side,and now you need to insert this image into a sql table.Create a stored procedure and insert easily this image into table.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Retrieving images from Database in C#

    If you want to retrieve the data from database and you are using front end as a C# and back end as a SQLServer .Absolutely,You can do this and I don't think any problem should be occur.

    private void button2_Click(object sender, EventArgs e)

    {
    const string conString = "Data Source=apex2006sql;Initial Catalog=ServManLeather;Integrated Security=True;";

    const string firequery = "Select ImgNumber, FacilityImg From FacilityImg Where ImgNumber = 1000";


    using (SqlConnection sqlconn = new SqlConnection(conString))

    {

    sqlconn.Open();
    using (SqlCommand comnd = new SqlCommand(firequery, sqlconn))

    {

    using (SqlDataReader rdr = comnd.ExecuteReader())

    {

    using (DataTable tbdata = new DataTable())

    {

    tbdata.Load(rdr);
    File.WriteAllBytes(@"C:\picture.bmp", (byte[])dt.Rows[0]["FacilityImg"]);

    }

    }

    }

    }

    }
    Last edited by Modifier; 25-01-2010 at 01:37 PM.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Inserting and retrieving images from Database

    An easily way to access the data from database,but its very primary and it's like introduction for you how would we get access to images from database.

    In your database,your images are stored in form of bytes and if you need to access them, you have to use fromstream method to covert it again into image.

    1. System.IO.MemoryStream memstream = new
    System.IO.MemoryStream(byte array of the image);

    and you would convert it into image to show on the screen

    2. System.Drawing.Image img =
    System.Drawing.Image.FromStream(memstream);

Similar Threads

  1. Replies: 2
    Last Post: 14-03-2012, 03:29 PM
  2. How to use ADO.NET Entity Framework to obtain images from database
    By Galbraith in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 02:10 AM
  3. Problem in retrieving timestamp
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 01:55 AM
  4. Inserting date into database
    By Xmen in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 02:36 PM
  5. Storing images in SQL database using Asp.net
    By Booth in forum Software Development
    Replies: 2
    Last Post: 17-01-2009, 06:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,469,391.57043 seconds with 17 queries