Accessing Attachments in an ADO.NET DataSet Example
Applies to: SharePoint Workspace 2010 | Visual Studio 2008
This C# code fragment demonstrates how to get the File Attachment metadata and contents for each data row in an ADO.NET DataSet converted from a Forms2RecordDataSet.
DataRow row = ...; // See Code fragment reading data rows
// Define array for File Attachment metadata
DataRow[] attachmentRows = new DataRow[0];
// Navigate DataSet to get from data row to rows
// with attachment metadata
// First get relationship created for Attachments field
if (row.Table.ChildRelations.Count > 0)
{
DataRelation relationForAttachProp = row.Table.ChildRelations[0];
DataRow[] attachPropRow = row.GetChildRows(relationForAttachProp);
DataRelation relationForFileAttach =
attachPropRow[0].Table.ChildRelations[0];
// Get the rows containing the File Attachment metadata
attachmentRows =
attachPropRow[0].GetChildRows(relationForFileAttach);
}
// Enumerate through the rows containing the File Attachment metadata
for (int n = 0; n < attachmentRows.GetLength(0); n++)
{
DataRow attachmentRow = attachmentRows[n];
// Get the metadata for the attachment
string fileName = (string)attachmentRow["FullName"];
int fileSize = (int)attachmentRow["Size"];
// Now get the attachment contents
byte[] contents = null;
// Navigate to row containing contents
DataRelation relationForContent =
attachmentRow.Table.ChildRelations[0];
if (attachmentRow.GetChildRows(relationForContent).Length == 1)
{
DataRow contentRow =
attachmentRow.GetChildRows(relationForContent)[0];
// Read the contents from Base64 column
contents = (byte[])contentRow["Base64"];
}
else
{
// Record was read without contents
}
}
See Also
Concepts
Using ADO.NET DataSets to Access Forms2RecordDataSet Data
Converting Forms2RecordDataSet to ADO.NET DataSet Example
Reading Data Records in an ADO.NET DataSet Example