Page tree
Skip to end of metadata
Go to start of metadata

Table of Contents

You can use a database table as a data source for a WordWriter template by passing the SetDataSource method a DataTable, a DataSet, an SqlDataReader, OleDbDataReader or an AdomdDataReader. If the specified data source contains more than one row, SetDataSource will use the first row of the first table as the data source. To import multiple rows from data sources use a repeat block.

Import the System.Data namespace for DataTable and DataSet objects. Import System.Data.SqlClient for SQL Server-specific database classes, or System.Data.OleDb for the OLEDB client and connections to other databases such as Microsoft Access .MDB files.

The following samples use SqlClient classes and connect to the AdventureWorks2000 database. These code samples use ADO.NET objects as data sources:

Code Samples

Code Sample: Purchase Order Document

[C#] | [VB.NET]

Code Sample: Mailing Labels

[C#] | [VB.NET]

Code Sample: Mail Merge

[C#] | [VB.NET]

Using an ADO.NET DataTable or DataSet as a Data Source

The following code example shows how to query the AdventureWorks2000 database and return the data as a DataSet object. The DataSet object is then used as the data source in a WordTemplate document:

C#
VB.NET

The first block of code in the GenerateDocument method connects to a database using a variable connString which represents a SQL Server connection string. Then, a SqlCommand is created and a parameter is added to the query. A SqlDataAdapter then executes the SqlCommand and fills a DataTable with the data. The next block of code shows how to use this DataTable with WordTemplate.

You can also use a DataSet as the data source. When using a DataSet, WordTemplate will use only the first DataTable in the DataSet.Tables collection.

Using an SqlDataReader, OleDbDataReader or AdomdDataReader as a Data Source

SqlDataReader, OleDbDataReaders and AdomdDataReader are also available for use by WordTemplate. Remember that unlike DataSet and DataTable objects, DataReader objects require an open connection to the database while reading data. Remember to close SqlDataReader, OleDbDataReader or AdomdDataReader objects calling WordTemplate.Process().

C#
VB.NET