0

I want to store image in a SQL Server database. I have converted image to byte array, and the data type of column in database is varbinary(MAX) but it didn't work even I have changed its type to Image but I get the same results.

I have followed many links from stackoverflow, code project, dream in code but couldn't find my solution there is my code for inserting byte array in database

string query = @"INSERT INTO myTable (ID, byteArray, DateTime) VALUES (@ID, @byteArray, @datetime)";

try
{
      command = new SqlCommand(query, base.conn);
      command.Parameters.AddWithValue("@ID", id);
      command.Parameters.AddWithValue("@byteArray", ss); // ss is byte[] from arguments
      command.Parameters.AddWithValue("@datetime", DateTime.Now);

      base.Open();
      if (command.ExecuteNonQuery() > 0)
      {
            base.Close();
            return true;
      }
      else
      {
             base.Close();
             return false;
      }
}
catch (SqlException ex)
{
      base.Close();
      return false;
}

I have also tried it with

command = new SqlCommand(query, base.conn);
command.Parameters.Add("@ID", id);
command.Parameters.Add("@byteArray", ss); // ss is byte[] from arguments
command.Parameters.Add("@datetime", DateTime.Now);

and the thing it will store in database

The 3rd Column is of Image type and i am sending byte array in it

but it will show the value in 3rd column as <Binary data> what is that???

1
  • @marc_s, that is the answer, if you posted that as-is I would have upvoted it. Commented Apr 15, 2014 at 18:51

1 Answer 1

1

SQL Server Management Studio will show <binary data> when some binary data (like a picture) is stored in that column. This is works as designed!

You won't actually see the picture itself in SQL Server Management Studio. I'm pretty sure your code works just fine - only your expectations of what Management Studio can do are too high ....

Sign up to request clarification or add additional context in comments.

2 Comments

but how do i retrieve it so that i can convert it in Bitmap
@assassino: you just use a standard query to SELECT the data and get it back to your .NET application as a byte array / stream, which you can then feed into the Bitmap class ... but that's a whole different question agaiN!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.