GroupDocs.Assembly for .NET seamlessly integrates with ADO.NET, allowing you to generate documents directly from database queries. You can use DataSet, DataTable, DataView, or IDataReader as data sources, making it easy to create reports from SQL Server, Oracle, MySQL, and other databases.
Here are the steps to generate documents from databases:
Connect to your database using ADO.NET
Execute SQL queries and populate DataSet or DataTable
Create a template document with expression tags referencing table and column names
Use DocumentAssembler to assemble the template with database data
Save the assembled document
Note
GroupDocs.Assembly supports all standard ADO.NET data sources, including DataSet, DataTable, DataView, DataRow, DataRowView, IDataReader, and IDataRecord.
Generate Document from DataTable
The simplest approach is to use a DataTable:
usingGroupDocs.Assembly;usingSystem.Data;usingSystem.Data.SqlClient;publicstaticvoidGenerateFromDataTable(){// Connect to database and populate DataTablestringconnectionString="Server=localhost;Database=MyDB;Integrated Security=true;";stringquery="SELECT CustomerName, Email, Balance FROM Customers";DataTablecustomersTable=newDataTable();using(SqlConnectionconnection=newSqlConnection(connectionString)){using(SqlCommandcommand=newSqlCommand(query,connection)){using(SqlDataAdapteradapter=newSqlDataAdapter(command)){adapter.Fill(customersTable);}}}// Assemble documentDocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("Template.docx","Output.docx",newDataSourceInfo(customersTable,"customers"));Console.WriteLine("Document generated from DataTable successfully.");}
For large datasets, use IDataReader for efficient streaming:
usingGroupDocs.Assembly;usingSystem.Data;usingSystem.Data.SqlClient;publicstaticvoidGenerateFromDataReader(){stringconnectionString="Server=localhost;Database=MyDB;Integrated Security=true;";stringquery="SELECT CustomerName, Email, Balance FROM Customers";using(SqlConnectionconnection=newSqlConnection(connectionString)){connection.Open();using(SqlCommandcommand=newSqlCommand(query,connection))using(SqlDataReaderreader=command.ExecuteReader()){DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("Template.docx","Output.docx",newDataSourceInfo(reader,"customers"));Console.WriteLine("Document generated from DataReader successfully.");}}}
Using DataView for Filtered Data
You can use DataView to filter and sort data before assembly:
usingGroupDocs.Assembly;usingSystem.Data;usingSystem.Data.SqlClient;publicstaticvoidGenerateFromDataView(){stringconnectionString="Server=localhost;Database=MyDB;Integrated Security=true;";DataTablecustomersTable=newDataTable();using(SqlConnectionconnection=newSqlConnection(connectionString)){using(SqlDataAdapteradapter=newSqlDataAdapter("SELECT * FROM Customers",connection)){adapter.Fill(customersTable);}}// Create filtered and sorted DataViewDataViewcustomersView=newDataView(customersTable);customersView.RowFilter="Balance > 1000";customersView.Sort="CustomerName ASC";DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("Template.docx","Output.docx",newDataSourceInfo(customersView,"customers"));Console.WriteLine("Document generated from DataView successfully.");}
Working with Related Tables
DataSet supports data relations for master-detail reports:
usingGroupDocs.Assembly;usingSystem.Data;usingSystem.Data.SqlClient;publicstaticvoidGenerateWithRelations(){stringconnectionString="Server=localhost;Database=MyDB;Integrated Security=true;";DataSetdataSet=newDataSet();using(SqlConnectionconnection=newSqlConnection(connectionString)){using(SqlDataAdapteradapter=newSqlDataAdapter("SELECT * FROM Customers",connection)){adapter.Fill(dataSet,"Customers");}using(SqlDataAdapteradapter=newSqlDataAdapter("SELECT * FROM Orders",connection)){adapter.Fill(dataSet,"Orders");}}// Create relationDataRelationrelation=newDataRelation("CustomerOrders",dataSet.Tables["Customers"].Columns["CustomerId"],dataSet.Tables["Orders"].Columns["CustomerId"]);dataSet.Relations.Add(relation);DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("Template.docx","Output.docx",newDataSourceInfo(dataSet.Tables["Customers"],"customers"));Console.WriteLine("Document generated with relations successfully.");}
When using database connections, always ensure proper disposal of resources. Use using statements or try-finally blocks to close connections and readers.
Advanced Usage Topics
To learn more about working with complex database queries, stored procedures, parameterized queries, and performance optimization for large datasets, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: