Search code examples
c#sql-serverblobdapper

How to read blob data from SQL Server Image field using Dapper?


I need to read blob data into a memory stream from an Image type column from an SQL Server database. How can I do this using Dapper?

I was reading the Dapper manual but was unable to find information about this.

UPDATE: I need to read the data from the database (from a query). All the links suggested so far has information about how to store the blob in the database.


Solution

  • Figured it out. The result dynamic type is a byte[].

    var row = con.QueryFirst("SELECT BLOBFIELD FROM TABLE WHERE ID = 1");
    
    byte[] bytes = drawings.BLOBFIELD;
    using (var stream = new System.IO.FileStream(@"C:\Temp\Test.dat", System.IO.FileMode.CreateNew))
      stream.Write(bytes, 0, bytes.Length);