2012-03-20 56 views
0

我有一个查询,其中我必须选择类型为字节的数据。在petapoco查询字节[]

byte[] data 

我的查询如下:

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

byte[] actual = file.GetData(); 

,我想找到的长度如下:

actual.Length 

上述的问题是,我一定要找到长度但GetData返回实际的动态对象。

如何检索数据并保存其长度? 有没有更好的方式来查询PetaPoco中的byte []?

+0

什么是db类型?你有错误吗? – Schotime 2012-03-21 09:59:12

回答

3

我只是尝试这样做:

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); 
} 

其中bytetable定义:

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

,它按预期工作。数据变量的确是一个字节[]。

+0

与上面我只能读取item.data,如何读取item.data的长度?数据被定义为varbinary(MAX)而不是图像 – learning 2012-03-21 12:50:08

+0

将其转换为字节[]? – Schotime 2012-03-22 04:11:04

+0

因为我已经更新了答案而用varbinary(max)工作过 – Schotime 2012-03-22 08:58:43