Table of Contents |
---|
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
Code Sample: Mailing Labels
Code Sample: Mail Merge
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:
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().