Search code examples
petapoco

query byte[] in petapoco


I have a query in which I have to select Data which is of type byte.

byte[] data

My query is as follows:

private IEnumerable<dynamic> GetData(int fileID){
    return Connection.db.Query<dynamic>("select Data from [File] where id = @0",  fileID.ToString());
}

byte[] actual = file.GetData();

and I want to find the length as follows:

actual.Length

The problem with the above is that I have to find the length but GetData returns a dynamic object actual.

How can I retrieve the Data and have its length? Is there a better way to query byte[] in PetaPoco?


Solution

  • I just tried this:

    var d = db.Fetch<dynamic>("select id, data from bytetable");
    foreach (var item in d)
    {
         Console.WriteLine(item.id + "-" + Encoding.ASCII.GetString(item.data) + "-" item.data.Length);
    }
    

    where bytetable is defined:

    create table bytetable (
        id int identity(1,1) primary key,
        data image (or varbinary(max)),
    )
    

    and it works as expected. The data variable is indeed a byte[].