开发者

Reading a blob field from database using IDataRecord

I need to populate a byte array from a database field using an IDataRecord and i needed help on how to get this done.

public class MyClass
{
   public string Name {get;set;}
   public byte[] ImageData { get; set;}
}

// Data Layer

开发者_StackOverflow社区
public MyClass Populate(IDataRecord dr)
{
   var myClass = new MyClass();
   myClass.Name = myDataRecord.GetString(myDataRecord.GetOrdinal("NAME"));
   myClass.ImageData = // Need info on how to load this

}

Thanks for any help


You can simply cast by using GetValue() method:

public MyClass Populate(IDataRecord dr)
{
   var myClass = new MyClass();

   int ordinal1 = myDataRecord.GetOrdinal("NAME");
   int ordinal2 = myDataRecord.GetOrdinal("IMAGEDATA");

   myClass.Name = myDataRecord.GetString(ordinal1);
   myClass.ImageData = (byte[])myDataRecord.GetValue(ordinal2);
}

EDIT: The GetOrdinal() is necessary for reading the ordinal of a field by name.


Is there some reason that you can't use IDataRecord.GetBytes as in

int imageDataOrdinal = myDataRecord.GetOrdinal("ImageData");
long bytesRead = myDataRecord.GetBytes(
                     imageDataOrdinal,
                     0,
                     myClass.ImageData,
                     0
                     length
                 );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜