Integrating with SQL Server or Access
In this section, you see the database link in action with InfoPath. When you choose the Database check, InfoPath requires a database connection, represented by an Office Database Connection (.odc) file, which contains connection information, keywords, and descriptions for the data connection. You can also provide a Microsoft Data Link (.udl) file (which is a standard file format created by Microsoft for specifying connection strings). In either case, InfoPath opens the personal My Data Source folder containing all your connections files. If you don’t have a connection file, you can create a new one by selecting the item +New SQL Server Connection.odc, as shown in Figure 7-1.Figure 7-1: Data source selection.
If you want to connect to a Microsoft Access database, you have to select your MDB file. Then InfoPath will provide the list of tables available in the MDB.
When you choose to create a new SQL Server connection, InfoPath starts the Data Connection Wizard, asking you the SQL Server database instance name and authentication data (i.e., username and password) to access to the database. If you have access to the database (you are authenticated), you have to choose the database and (optionally) the table name, as shown in Figure 7-2.
Figure 7-2: Database and table selection.
If you deselect the Connect to a specific table check box, your data connection file will be data-table-independent, and then you are free to define the table later during the template design phase. The final box in the wizard is the filename and properties box (see Figure 7-3).
Figure 7-3: Database connection file properties.
Note that if you keep Save password in file unchecked, you will always be prompted to insert the password for database connection. Otherwise, if you check it, the password will be saved in a clear form into the ODC file, exposing you to a high security risk. The best solution is to use a trusted connection where the current logged user will be the user logged in to the database.Going ahead, you have to choose the table you want to bind to your data form (see Figure 7-4).
Figure 7-4: Table selection.
If you added the table name during the data file connection, the table selection box will not appear. The next step is to choose the table fields you want to use for the template (see Figure 7-5).
Figure 7-5: Table fields selection.
This table fields box gives you rich support for creating more complex table relations. For example, in Figure 7-5 the Employees table of the Northwind database is selected. You could also ask for all the orders of the employee. This can be done by adding a new table, Orders, and defining the fields relationship, EmployeeID. InfoPath then creates a hierarchy with Employees as parent and Orders as child (see Figure 7-6).
Figure 7-6: Data table relationship.
The last step is to choose whether to display the query view or data view first when the user opens InfoPath for the first time. By default, the query view is filled, while the data view is empty. You can create the data view from the data source, as you have seen in Chapter 1. If you following the preceding steps, the form is automatically linked to the database table chosen, and you can read, create, delete, and update records in the database when you submit the form. InfoPath does everything for you transparently.There are other scenarios where you have to submit the data in a different way—for example, through a stored procedure. That is a case where you have to implement some script code by yourself.