Save from SQL table to text file through data table and stream record

thats my code

static void Write(DataTable dt, string outputFilePath)
        {
        int[] maxLengths = new int[dt.Columns.Count];

        for (int i = 0; i < dt.Columns.Count; i++)
        {
            maxLengths[i] = dt.Columns[i].ColumnName.Length;

            foreach (DataRow row in dt.Rows)
            {
                if (!row.IsNull(i))
                {
                    int length = row[i].ToString().Length;

                    if (length > maxLengths[i])
                    {
                        maxLengths[i] = length;
                    }
                }
            }
        }

        using (StreamWriter sw = new StreamWriter(outputFilePath, false))
        {   
            sw.Write(new byte[]{0xff,0xfe});
            foreach (DataRow row in dt.Rows)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (!row.IsNull(i))
                    {
                        sw.Write(row[i].ToString() + "  ");
                    }
                }

                sw.WriteLine();
            }
        }
    }

I want to put 2 bytes in a text file at the beginning.

sw.Write(new byte[]{0xff,0xfe}); 

The problem is that I open with hexadecimal, I do not see these 2 bytes. but I see in the text file these http://img713.imageshack.us/img713/1143/zaqj.png: \

any solution please help. suppose it looks likehttp://img266.imageshack.us/img266/1105/y40i.png

-1
source share
1 answer

try like this:

sw.Write(new char[]{(char)0xff , (char)0xfe});

there is no Write implementation with an array of bytes in the parameters: http://msdn.microsoft.com/en-us/library/system.io.streamwriter.write.aspx

therefore, it uses the Object parameter and calls its ToString () method

0
source

All Articles