developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




Vote for the Developer.com Product of the Year Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
The Year of Living the OpenSocial    November 14, 2008
Firefox Fixes New and Older Versions    November 13, 2008
Can Apache Maven Make It by Going Commercial?    November 12, 2008
Novell Goes After Red Hat Linux Users    November 11, 2008
Free Tech Newsletter -

Saving Rich Edit Control Text to SQL Server
By Tom Archer

Go to page: Prev  1  2  

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.

Go to page: Prev  1  2  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


.NET Archives






internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers