FieldDatabaseQuery Property

Gets or sets a set of SQL instructions that query the database.

Namespace:  Aspose.Words.Fields
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public string Query { get; set; }

Property Value

Type: String
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