July 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Saving Rich Edit Control Text to SQL Server

  • June 28, 2004
  • By Tom Archer
  • Send Email »
  • More Articles »

Reading RTF Data from SQL Server

Reading the RTF data is even easier than saving it as no intermediary file is necessary. The first thing I do is to create the SqlConnection object the same way it was instantiated in the btnSave_Click method. I then instantiate a SqlCommand object, where I specify that I wish to read the Photo column from the Employees table where the EmployeeID value is equal to 1. The SqlCommand.ExecuteReader method executes the command and returns a SqlDataReader object. A call to SqlDataReader.Read should return the only record that matches my criteria. Just to be sure that valid data has been read, I then check the SqlDataReader.HasRows property and verify that the Photo column I'm reading is not null. Otherwise, an exception would be thrown when I attempt to read it.

Once I've determined that I have valid data, I then use the SqlDataReader.GetBytes method to read the binary data into a Byte array. You'll notice that there are actually two calls to GetBytes in the code sample below. I did this because GetBytes cannot be called to retrieve the data into a buffer until that buffer has been allocated. However, I know how big to make the buffer only after calling GetBytes! Therefore, the first call to GetBytes passes a null value for the object that is to receive the data, meaning that this call will return to me only the number of bytes contained in the Photo column. I then allocate the Byte array and call GetBytes a second time—this time passing the Byte array and receiving the data. Once the data is in the buffer, I can then use the ASCIIEncoding object to convert the data from a Byte array into a String object, which is finally used to update the rich-edit control:

private void btnRead_Click(object sender, System.EventArgs e)
{
  richTextBox1.Clear();

  SqlConnection cn = null;
  SqlCommand cmd = null;
  SqlDataReader reader = null;
  try
  {
    cn = new SqlConnection("Database=Northwind;Integrated Security=true;");
    cn.Open();
    cmd = new SqlCommand("SELECT Photo FROM Employees WHERE EmployeeID=1", cn);
    reader = cmd.ExecuteReader();
    reader.Read();
    if (reader.HasRows)
    {
      if (!reader.IsDBNull(0))
      {
        Byte[] rtf = new Byte[Convert.ToInt32((reader.GetBytes(0, 0, null, 0, Int32.MaxValue)))];
        long bytesReceived = reader.GetBytes(0, 0, rtf, 0, rtf.Length);
  
        ASCIIEncoding encoding = new ASCIIEncoding();
        richTextBox1.Rtf = encoding.GetString(rtf, 0, Convert.ToInt32(bytesReceived));
      }
    }
  }
  catch(Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
  finally
  {
    if (null != reader) reader.Close();
    if (null != cn) cn.Close();
  }
}

Download the Code

To download the accompanying source code for this tip, click here.

About the Author

The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel