Visual C# Developer Center > Visual C# Forums > Visual C# General > getting binary data out of the database
Ask a questionAsk a question
 

Proposed Answergetting binary data out of the database

  • Saturday, November 07, 2009 10:19 AMMitja Bonca Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I am doing an app which inclueds richTextBox, for inserting text. I put some text into richTextBox and save it into my sql database (column is varbinary(MAX) data type, with this code
    byte[] myFile = Encoding.UTF8.GetBytes(richTextBox1.Text);
    
    Here the text is changed into binary data, and them I add parameters and all whats needed, and finally:
    cmd1.ExecuteNonQuery();
    

    Then, when I want to open it, it opens in word (the code is made, that it opens in this program - depending on an extention and a file type).

    So, when the Word is starting up, 1st it asks me if I want to change it (to use some other coding). I select nothing and click Ok. The text is there but it is not in the shape I inserted it into richTextBox. The font are all the same and are in Courier New (I didn`t save the the text in with this font, and even the text was not all the same).

    Anyone has any clue how to get the same text out, as I saw inserted into richTextBox?  Is this maybe anything to do with richTextBox, or this is only a matter of coding?

All Replies

  • Saturday, November 07, 2009 5:59 PMtechnocrat_aspire Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can you post the code to get the binary data as well? How are you retrieving it?
  • Saturday, November 07, 2009 6:31 PMMitja Bonca Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    This is my code:

     povezava.Open();
                string dobiVsebino = "SELECT VsebinaO FROM Obvestilo WHERE ImeO = '" + listView1.SelectedItems[0].Text + "'";
                SqlCommand cmd1 = new SqlCommand(dobiVsebino, povezava);
                byte[] buffer = (byte[])cmd1.ExecuteScalar();
                povezava.Close();
    
    This is my exact code which gets the binary data out of the database.
  • Tuesday, November 10, 2009 12:50 PMRoahn LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Hello,

    I'm not sure how did you open it in a word, have you tried encoding the byte array into a string and then display?
                String myText = Encoding.UTF8.GetString(buffer);
                this.richTextBox1.Text = myText;

    Please let me know if this helps.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!