FieldDatabase Class
Implements the DATABASE field.
Inheritance Hierarchy
SystemObject
  Aspose.Words.FieldsField
    Aspose.Words.FieldsFieldDatabase

Namespace: Aspose.Words.Fields
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.11
Syntax
public class FieldDatabase : Field

The FieldDatabase type exposes the following members.

Constructors
  NameDescription
Public methodFieldDatabase
Initializes a new instance of the FieldDatabase class
Properties
  NameDescription
Public propertyCode exampleConnection
Gets or sets a connection to the data.
Public propertyCode exampleDisplayResult
Gets the text that represents the displayed field result.
(Inherited from Field.)
Public propertyCode exampleEnd
Gets the node that represents the field end.
(Inherited from Field.)
Public propertyCode exampleFileName
Gets or sets the complete path and file name of the database
Public propertyCode exampleFirstRecord
Gets or sets the integral record number of the first data record to insert.
Public propertyCode exampleFormat
Gets a FieldFormat object that provides typed access to field's formatting.
(Inherited from Field.)
Public propertyCode exampleFormatAttributes
Gets or sets which attributes of the format are to be applied to the table.
Public propertyCode exampleInsertHeadings
Gets or sets whether to insert the field names from the database as column headings in the resulting table.
Public propertyCode exampleInsertOnceOnMailMerge
Gets or sets whether to insert data at the beginning of a merge.
Public propertyCode exampleIsDirty
Gets or sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
(Inherited from Field.)
Public propertyCode exampleIsLocked
Gets or sets whether the field is locked (should not recalculate its result).
(Inherited from Field.)
Public propertyCode exampleLastRecord
Gets or sets the integral record number of the last data record to insert.
Public propertyCode exampleLocaleId
Gets or sets the LCID of the field.
(Inherited from Field.)
Public propertyCode exampleQuery
Gets or sets a set of SQL instructions that query the database.
Public propertyCode exampleResult
Gets or sets text that is between the field separator and field end.
(Inherited from Field.)
Public propertyCode exampleSeparator
Gets the node that represents the field separator. Can be null.
(Inherited from Field.)
Public propertyCode exampleStart
Gets the node that represents the start of the field.
(Inherited from Field.)
Public propertyCode exampleTableFormat
Gets or sets the format that is to be applied to the result of the database query.
Public propertyCode exampleType
Gets the Microsoft Word field type.
(Inherited from Field.)
Methods
  NameDescription
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodCode exampleGetFieldCode
Returns text between field start and field separator (or field end if there is no separator). Both field code and field result of child fields are included.
(Inherited from Field.)
Public methodCode exampleGetFieldCode(Boolean)
Returns text between field start and field separator (or field end if there is no separator).
(Inherited from Field.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Protected methodNeedStoreOldResultNodes (Inherited from Field.)
Public methodCode exampleRemove
Removes the field from the document. Returns a node right after the field. If the field's end is the last child of its parent node, returns its parent paragraph. If the field is already removed, returns null.
(Inherited from Field.)
Public methodToString (Inherited from Object.)
Public methodCode exampleUnlink
Performs the field unlink.
(Inherited from Field.)
Public methodCode exampleUpdate
Performs the field update. Throws if the field is being updated already.
(Inherited from Field.)
Public methodCode exampleUpdate(Boolean)
Performs a field update. Throws if the field is being updated already.
(Inherited from Field.)
Remarks
Inserts the results of a database query into a WordprocessingML table.
Examples
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Use a document builder to insert a database field
FieldDatabase field = (FieldDatabase)builder.InsertField(FieldType.FieldDatabase, true);

// Create a simple query that extracts one table from the database
field.FileName = MyDir + @"Database\Northwind.mdb";
field.Connection = "DSN=MS Access Databases";
field.Query = "SELECT * FROM [Products]";

// Insert another database field
field = (FieldDatabase)builder.InsertField(FieldType.FieldDatabase, true);
field.FileName = MyDir + @"Database\Northwind.mdb";
field.Connection = "DSN=MS Access Databases";

// This query will sort all the products by their gross sales in descending order
field.Query =
    "SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
    "FROM([Products] " +
    "LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
    "GROUP BY[Products].ProductName " +
    "ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC";

// You can use these variables instead of a LIMIT clause, to simplify your query
// In this case we are taking the first 10 values of the result of our query
field.FirstRecord = "1";
field.LastRecord = "10";

// The number we put here is the index of the format we want to use for our table
// The list of table formats is in the "Table AutoFormat..." menu we find in MS Word when we create a data table field
// Index "10" corresponds to the "Colorful 3" format
field.TableFormat = "10";

// This attribute decides which elements of the table format we picked above we incorporate into our table
// The number we use is a sum of a combination of values corresponding to which elements we choose
// 63 represents borders (1) + shading (2) + font (4) + colour (8) + autofit (16) + heading rows (32)
field.FormatAttributes = "63";

field.InsertHeadings = true;
field.InsertOnceOnMailMerge = true;

doc.UpdateFields();
doc.Save(ArtifactsDir + "Field.Database.docx");
See Also