How to: Populate Documents with Data from a Database
You can access data in document-level projects for Microsoft Office in the same way that you access data in Windows Forms projects. You use the same tools and code to bring the data from a database into your solution, and you can use Windows Forms controls to display the data.
In addition, you can display data by using host controls. Host controls are native objects in Microsoft Office Word that have been enhanced with events and data binding capability. For more information, see Host Items and Host Controls Overview.
Applies to: The information in this topic applies to document-level projects for Word. For more information, see Features Available by Office Application and Project Type.
The following example shows how to add data-bound controls in document-level projects using a designer. For an example of how to add data-bound controls in VSTO Add-in projects at run time, see Walkthrough: Simple Data Binding in VSTO add-in Project.
For a related video demonstration, see Binding Data to Word 2007 Content Controls Using Visual Studio Tools for the Office System (3.0).
Open a Word document-level project in Visual Studio, with the document open in the designer.
Open the Data Sources window and create a data source from a database. For more information, see How to: Connect to Data in a Database.
Drag the field you want from the Data Sources window to your document.
A content control is added to the document. The type of content control depends on the data type of the field you selected. For more information, see Content Controls.
You can add a different control by selecting the data field in the Data Sources window and then choosing a different control from the drop-down list.
In addition to the control, the following data-related objects are automatically added to your project:
A typed dataset that encapsulates the data tables that you connected to in the database. For more information, see Dataset tools in Visual Studio.
A TableAdapter that connects the typed dataset to the database. For more information, see TableAdapter Overview.
A TableAdapterManager, which is used to coordinate table adapters in the dataset to enable hierarchical updates. For more information, see Hierarchical Update and TableAdapterManager Overview.
When you run the project, the control displays the first record in the data source. You can use the BindingSource to enable users to scroll through the records.
For information about how to send updates to the typed dataset and the database, see How to: Update a Data Source with Data from a Host Control.
Binding Data to Controls in Office Solutions
Add new data sources
Binding Windows Forms controls to data in Visual Studio
How to: Populate Documents with Data from Objects
How to: Update a Data Source with Data from a Host Control
Using Local Database Files in Office Solutions Overview
Connecting to Data in Windows Forms Applications
BindingSource Component Overview