Reading Images Stored In SQL Database (C#) 500pts

I am having an issue getting images I am storing in an SQL database. For some reason, I always get back a byte[] with a length of 6. Below is the code I am using for both the storing and retrieving of the data:

STORING:


private void storeImageCapturePicture(string filePath, string recorderName, string cameraName)
        {
            try
            {
                SqlConnection dataConnection = new SqlConnection(sqlConnectionString);
                dataConnection.Open();

                Image image = Image.FromFile(filePath);
                FileInfo fileInfo = new FileInfo(filePath);
                long bytes = fileInfo.Length;
                FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryReader binaryReader = new BinaryReader(fileStream);
                byte[] imageData = binaryReader.ReadBytes((int)bytes);

                string commandString = ("UPDATE Cameras SET Image = " + imageData + ", ImageSize = " + imageData.Length + " WHERE RecorderName = '" + recorderName + "' and CameraName = '" + cameraName + "'");
                SqlCommand command = new SqlCommand(commandString, dataConnection);
                command.ExecuteNonQuery();

                image.Dispose();
                fileStream.Close();
                binaryReader.Close();
                dataConnection.Close();

                File.Delete(filePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


GETTING:


SqlConnection dataConnection = new SqlConnection(sqlConnectionString);
                dataConnection.Open();
                string commandString = ("SELECT Image, ImageSize FROM Cameras WHERE RecorderName = '" + recorderName + "' and CameraName = '" + cameraName + "'");
                SqlCommand command = new SqlCommand(commandString, dataConnection);
                SqlDataReader dataReader = command.ExecuteReader();

                while (dataReader.Read())
                {
                    byte[] imageRetrieved = (byte[])dataReader["Image"];
                }

Also note that when getting the image I am simply using a breakpoint directly after the last statement and checking the contents of the byte array, which always has a length of 6. I can go in the database and see that the "ImageSize" column has the correct number (the images are typically about 125KB). I have also tried using code I have found all over the net but no matter what I try I just get the length 6 byte array size. Please help! Thanks.
Pretzel_JesusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale BurrellDirectorCommented:
I think when you are updating the the record you can't just add the byte array as if it were a string... you could always print out the update statement and see exactly what it is doing.

Personally whenever I deal with image data I use SqlParameters to pass the data in.
0
AshokCommented:
STORING:


private void storeImageCapturePicture(string filePath, string recorderName, string cameraName)
{
  try
  {
    SqlConnection dataConnection = new SqlConnection(sqlConnectionString);
    dataConnection.Open();

    FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    Byte[] imageData = new Byte[fileStream.Length];
    fileStream.Read(imageData, 0, (imageData.Length);
    fileStream.Close();

    string commandString = ("UPDATE Cameras SET Image = " + imageData + ", ImageSize = " +
                            imageData.Length + " WHERE RecorderName = '" + recorderName +
                            "' and CameraName = '" + cameraName + "'");
    SqlCommand command = new SqlCommand(commandString, dataConnection);
    command.ExecuteNonQuery();

    image.Dispose();
    fileStream.Close();
    binaryReader.Close();
    dataConnection.Close();

    File.Delete(filePath);
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.ToString());
  }
}

GETTING:

SqlConnection dataConnection = new SqlConnection(sqlConnectionString);
dataConnection.Open();
string commandString = ("SELECT Image, ImageSize FROM Cameras WHERE RecorderName = '" + recorderName + "' and CameraName = '" + cameraName + "'");
SqlCommand command = new SqlCommand(commandString, dataConnection);
SqlDataReader dataReader = command.ExecuteReader();


dataReader.Read();  // Read one record (no need for loop if you know that there is only one record returned.
//byte[] imageRetrieved = (byte[])dataReader["Image"];
Byte[] imageRetrieved = new Byte[0];
imageRetrieved = (Byte[])dataReader["Image"]);
MemoryStream ms = new MemoryStream(imageRetrieved);
pictureBox1.Image = Image.FromStream(ms);

dataConnection.Close();

HTH
Ashok
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AshokCommented:
STORING:

private void storeImageCapturePicture(string filePath, string recorderName, string cameraName)
{
  try
  {
    SqlConnection dataConnection = new SqlConnection(sqlConnectionString);
    dataConnection.Open();

    FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    Byte[] imageData = new Byte[fileStream.Length];
    fileStream.Read(imageData, 0, (imageData.Length);
    fileStream.Close();

    string commandString = ("UPDATE Cameras SET Image = " + imageData + ", ImageSize = " +
                            imageData.Length + " WHERE RecorderName = '" + recorderName +
                            "' and CameraName = '" + cameraName + "'");
    SqlCommand command = new SqlCommand(commandString, dataConnection);
    command.ExecuteNonQuery();

    dataConnection.Close();

    File.Delete(filePath);
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.ToString());
  }
}

GETTING:

  SqlConnection dataConnection = new SqlConnection(sqlConnectionString);
  dataConnection.Open();
  string commandString = ("SELECT Image, ImageSize FROM Cameras WHERE RecorderName = '" + recorderName + "' and CameraName = '" + cameraName + "'");
  SqlCommand command = new SqlCommand(commandString, dataConnection);
  SqlDataReader dataReader = command.ExecuteReader();


  dataReader.Read();  // Read one record (no need for loop if you know that there is only one record returned.
  //byte[] imageRetrieved = (byte[])dataReader["Image"];
  Byte[] imageRetrieved = new Byte[0];
  imageRetrieved = (Byte[])dataReader["Image"]);
  MemoryStream ms = new MemoryStream(imageRetrieved);
  pictureBox1.Image = Image.FromStream(ms);

  dataConnection.Close();

HTH
Ashok
0
Pretzel_JesusAuthor Commented:
You guys were both right. Thank you Ashok for reordering my code and I can't believe I was using byte[] instead of Byte[]... it was a long night :). It also wouldn't work until I stored the byte array using a parameter like Dale suggested. Now it's working fine! Thanks guys.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.