Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top