开发者

System.Text.Encoding.ASCII.GetBytes loses first two bytes from each string

I'm attempting to turn the body of an email from Exchange Web Services email into a byte array, but every time I do this, I'm missing the first two characters of the text after inserting it into an SQL database field with the type of image

byte[] emBodyBytes;

foreach (Item item in findResults)
{
 if (item is EmailMessage)
 {
   EmailMessage em = item as EmailMessage;
   emBodyBytes = System.Text.Encoding.ASCII.GetBytes(em.Body.Text.ToString());

   dEmailMessage thisMailMessage = new dEmailMessage  //LINQ-to-SQL
   {
    emmMessage = emBodyBytes   //This is our byte object of the email body string
   }
   emailDB.dEmailMessages.InsertOnSubmit(thisMailMessage);
  }
}

So The quick brown fox becomes e quick brown fox when it's inserted into the SQL image field.

Where I'm a bit lost is if it's something happening during开发者_运维知识库 the conversion process, or if SQL is expecting leading two bytes for a field with an image definition.

The definition of the field is

CREATE TABLE [dbo].[dEmailMessages](
    [emmMessage] [image] NOT NULL
)


Maybe immediately after the System.Text.Encoding.ASCII.GetBytes call, add something like :

System.IO.File.WriteAllBytes("c:\\SomeFile.txt", emBodyBytes) 

then have a look at the generated file in a hex viewer. See if the missing two bytes are there at that stage. At least this allows you to verify if the GetBytes call is behaving as you expect it to.


The essential code points are missing. I'd recommend to simplify by abandoning LINQ to SQL and writing out the SELECT statement and walking the reader and see if that helps. It may not but will make diagnostic easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜