[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 687
  • Last Modified:

Save images to local disk from a database

Dear Experts,
I have a table containing employees information like Employee ID and Employee Photo
I need to get all the employees photos which stored in a binary filelds in the database and save them to my local hard disk, each photo should take the name of the Employee ID.
am using C# in my ASP.NET Project
thanks ..
0
eghanem
Asked:
eghanem
1 Solution
 
DropZoneCommented:
So what is your question?  Do you need help retrieving the data, or saving it to a file, or both?

Here's some information on this:
http://www.codeproject.com/cs/database/ImageSaveInDataBase.asp

private void retrieveImages()
{
    try
    {
        byte[] EmpImage;
        int EmpID;
        FileStream fs;

        // replace _CONNECTION_STRING_ with your own.
        SqlConnection MyConnection = new SqlConnection(_CONNECTION_STRING_);

        // replace query with your own
        SqlCommand MyCommand = new SqlCommand("SELECT emp_id, emp_img FROM employee", MyConnection);

        MyConnection.Open();
        SqlReader MyReader = MyCommand.ExecuteReader()

        // loop through all the records and save the images
        while (MyReader.Read())
        {
            EmpId = (int) MyReader["emp_id"];
            EmpImage = (byte[]) MyReader["emp_img"];

            // change the extension to the appropriate one
            fs = new FileStream(EmpId.ToString + '.jpg', FileMode.CreateNew, FileAccess.Write);
            fs.Write(EmpImage, 0, EmpImage.Length);
            fs.Flush();
            fs.Close();

            MyReader.ReadNext();
        }

    }
    catch(Exception ex)
    {
        // handle exceptions
    }
    finally
    {
        MyConnection.Close();
    }
}
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now