0

This is how our data is kept in the DB:

enter image description here

I'm getting the DB table in a datatable and looping through. The same data is implicitly converted to a byte array. This is from debug session:

enter image description here

I want the data as is. I will write it to a file. Currently I can access to bytearray with row.ItemArray[0]. Its value is {byte[70]}. There's no way to use index on row.ItemArray[0]. row.ItemArray[0][0] does not give you 86. I don't need 86 anyway. If I write row.ItemArray[0] to a file, it looks System.Byte[]

Jude
  • 2,353
  • 10
  • 46
  • 70

1 Answers1

0

Why are you converting binary data to a string. It will never work. Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.dat";
        static void Main(string[] args)
        {
            DataTable datatable = new DataTable(); //just for testing, use your current table
            FileStream stream = new FileStream(FILENAME, FileMode.Create);
            BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8);
            foreach (DataRow row in datatable.AsEnumerable())
            {
                writer.Write(row.Field<byte[]>(0));
            }
            writer.Flush();
            writer.Close();
        }

    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • OP states (in comments) that he wants data in file to look exactly like in SSMS, so - as a hex string. – Evk Dec 27 '16 at 12:04
  • It is not a hex string!!! it start with 0x......... The code I posted will not change data and will work with any data type. – jdweng Dec 27 '16 at 12:07
  • I mean in SSMS display it looks like hex string (with "0x", but that doesn't matter) and OP wants it to look exactly the same in a TEXT file (despite it being byte array in database). – Evk Dec 27 '16 at 12:13
  • The title says "keep it as it is". Changing from bytes to characters is not keeping it as is. – jdweng Dec 27 '16 at 13:34