Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.37301.10 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert_Database_into_Dataset_and_Perform_Mailmerge", "Convert_Database_into_Dataset_and_Perform_Mailmerge\Convert_Database_into_Dataset_and_Perform_Mailmerge.csproj", "{F466E5AF-87B2-BED0-A680-8B3305676943}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F466E5AF-87B2-BED0-A680-8B3305676943}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F466E5AF-87B2-BED0-A680-8B3305676943}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F466E5AF-87B2-BED0-A680-8B3305676943}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F466E5AF-87B2-BED0-A680-8B3305676943}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3CD5B0D7-D2A2-4D43-8C8D-3239CD3DE50D}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
<PackageReference Include="System.Data.OleDb" Version="*" />
</ItemGroup>
<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.Collections;
using System.Data;
using System.Data.OleDb;

namespace Convert_Database_into_Dataset_and_Perform_Mailmerge
{
class Program
{
static void Main(string[] args)
{
// Load the word document
using (FileStream sourceStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Creating a new document.
using (WordDocument document = new WordDocument(sourceStreamPath, FormatType.Docx))
{

string dataBase = Path.GetFullPath(@"../../../Data/EmployeeDetails.mdb");

// Get all data
DataSet ds = GetAllTables(dataBase);
//ArrayList contains the list of commands
ArrayList commands = GetCommands();
//Executes the mail merge
document.MailMerge.ExecuteNestedGroup(ds, commands);
//Saves the Word document to MemoryStream.
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
document.Save(outputStream, FormatType.Docx);
}
}
}
}

/// <summary>
/// Get the commands to execute with database.
/// </summary>
static ArrayList GetCommands()
{
//ArrayList contains the list of commands
ArrayList commands = new ArrayList();

// Parent table: Employees (no filter, so empty string)
commands.Add(new DictionaryEntry("Employees", ""));

// Customers filtered by EmployeeID
commands.Add(new DictionaryEntry("Customers", "EmployeeID = %Employees.EmployeeID%"));

// Orders filtered by CustomerID
commands.Add(new DictionaryEntry("Orders", "CustomerID = %Customers.CustomerID%"));

return commands;
}
//Retrieves all required tables from the MDB database and prepares hierarchy commands for DocIO mail merge.
static DataSet GetAllTables(string dataBase)
{
// Connection string using ACE OLEDB provider (64-bit compatible)
string connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={dataBase};Persist Security Info=False;";
// DataSet to hold all tables
DataSet ds = new DataSet();

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
// Tables to fetch from MDB
string[] tables = { "Employees", "Customers", "Orders" };
// Loop through each table and fill DataSet
foreach (string tableName in tables)
{
string sqlQuery = $"SELECT * FROM {tableName}";
OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery, conn);
adapter.Fill(ds, tableName);
}
}
return ds;
}
}
}
Loading