Assassino
Assassino

Reputation: 31

Store Bitmap converted to byte array in Varbinary(MAX) or Image data type of SQL Server

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???

Upvotes: 0

Views: 4050

Answers (1)

marc_s
marc_s

Reputation: 754240

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 ....

Upvotes: 1

Related Questions