문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top