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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AshokConnect With a Mentor Commented:
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
 
Dale BurrellConnect With a Mentor DirectorCommented:
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();

    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
All Courses

From novice to tech pro — start learning today.