views:

26

answers:

1

I'm trying to write a test that returns a data-reader with one of the columns being a byte[]. I figured I can create a datatable and create a reader from that.

var cboTable = new DataTable("CBOTable");
var colValue = new SqlBinary(ASCII.GetBytes("Hello This is test"));

cboTable.Columns.Add("ByteArrayColumn");
cboTable.Rows.Add(colValue);

var reader= cboTable.CreateDataReader();

the problem is when I add colValue to the datarow instead of adding it as a byte[] it adds it to the row as it's string representation which is "SqlBinary(18)".

my question is how do I add an actual byte[] to my datarow

+1  A: 

According to MSDN:

If you are creating a DataTable programmatically, you must first define its schema by adding DataColumn objects to the DataColumnCollection (accessed through the Columns property). For more information about adding DataColumn objects, see Adding Columns to a DataTable (ADO.NET).

By writing cboTable.Columns.Add("ByteArrayColumn");, you've defined a column name, but not given it a type, so it defaults to being a "string" column. You should follow the example in the doc in order to create the table schema before adding data to the table.

JaredReisinger
cboTable.Columns.Add("ByteArrayProp",typeof(SqlBinary)); was the right way to go about it.
Keivan