0

I have the following interface:

IEnumerator<SqlDataRecord> IEnumerable<SqlDataRecord>.GetEnumerator() 
{
    var SQLRow = new SqlDataRecord(
        new SqlMetaData("id", SqlDbType.BigInt),
        new SqlMetaData("image", SqlDbType.Image));

    foreach (ImageModel item in this)
    {
        SQLRow.SetSqlInt64(0, item.Id);
        // here is the problem (this is a byte[] variable)
        SQLRow.SetSqlByte(1, item.Image); 

        yield return SQLRow;
    }
}

So how can i map the byte[] to Image? Or maybe i can store the image in a different way?

Thank you.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Cristian Szpisjak
  • 2,429
  • 2
  • 19
  • 32

2 Answers2

1

You should put new SqlDataRecord(..) inside foreach statement, otherwise it will return the same object all the time and just overrides the values. Not sure how do you use it but it might be a problem.

To set the value use SQLRow.SetSqlBytes(1, new SqlBytes(...)); or SQLRow.SetSqlBinary(...) or SQLRow.SetBytes(...)

It is better if you use varbinary(max) or varbinary(YOUR MAX LENGTH) type for storing your data as the image type is obsolete in MS SQL.

Sergey L
  • 1,402
  • 1
  • 9
  • 11
0

You can save the byte array as it is in SQL Server.This way its easier to retrieve back in your application.

Naganathan
  • 181
  • 1
  • 1
  • 4
  • OK. How about performance issues. If i save it as a `NVARCHAR(MAX)` vs `Image` what is the downside? – Cristian Szpisjak Apr 20 '16 at 10:30
  • Using byte[] will yield performance gain over nvarchar(max)..nvarchar is meant for string with support for different encodings.For your case you can refer to the post http://stackoverflow.com/questions/25400555/save-and-retrieve-image-binary-from-sql-server-using-entity-framework-6 – Naganathan Apr 20 '16 at 10:46