简体   繁体   中英

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

第三列是图像类型,我正在其中发送字节数组

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM