Cristian Szpisjak
Cristian Szpisjak

Reputation: 2469

How can i map a byte[] value to SQL type Image?

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.

Upvotes: 1

Views: 1054

Answers (2)

Sergey L
Sergey L

Reputation: 1492

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.

Upvotes: 2

Naganathan
Naganathan

Reputation: 181

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

Upvotes: 0

Related Questions