Skip to end of metadata
Go to start of metadata

Code Sample: Importing from a Database

[C#][VB.NET]

The sample above generates a spreadsheet from the template using ADO.NET objects as the data source.

The template contains the following data markers:

Data Markers

These data markers will be populated in the ASP.NET code by two database data sources. The data source for the %%=Employee.* data markers is an ADO.NET DataTable, and the data source for the %%=Orders.* data markers is an ADO.NET DataSet.

To set a template data source to a DataSet or DataTable using OleDB, import the System.Data and System.Data.OleDB namespaces to the ASP.NET page. For example:

 

Setting a Data Source to a DataSet with OleDB

To create a DataSet to use as a data source:

  1. Create an OleDbConnection object to open a database connection.

  2. Create a SQL query to get data from the database.

  3. Create an OleDbDataAdapter that will execute the SQL command at the data source and fill the DataSet.

  4. Create a DataSet and use the OleDbDataAdapter to fill it with the data retrieved from the database.

For example, in Databasebind.aspx, the following code creates and fills OrdersDs, the DataSet which will be used as the data source for the set of %%=Orders.* data markers.

To bind a DataSet to a template data marker, call [BindData|ExcelTemplate.BindData] using the following signature:

This signature takes a DataSet, the name of the data marker to which the data source should bind, and a collection of binding property values. In Databasebind.aspx, this BindData call binds the DataSet OrdersDs to the data markers *%%=Orders.OrderID*, *%%=Orders.Customer*, *%%=Orders.OrderDate*, and *%%=Orders.OrderTotal* in the template:

 

Setting a Data Source to a DataTable with OleDB

To create a DataTable to use as a data source:

  1. Create an OleDbConnection object to open a database connection.

  2. Create a SQL query to get data from the database.

  3. Create an OleDbDataAdapter that will execute the SQL command at the data source and fill the DataTable.

  4. Create a DataTable and use the OleDbDataAdapter to fill it with the data retrieved from the database.

 

For example, in Databasebind.aspx, the following code creates and fills EmployeeDt, the DataTable which will be used as the data source for the set of %%=Employee.* data markers.

To bind a DataTable to a template data marker, call [BindData|ExcelTemplate.BindData] using the following signature:

 

This signature takes a DataTable, the name of the data marker to which the data source should bind, and a collection of binding property values. In Databasebind.aspx, this BindData call binds the DataTable EmployeeDt to the data markers *%%=Employee.Name* and *%%=Employee.Title* in the template:

 

 

 

  • No labels