GroupDocs.Parser provides the functionality to extract data from databases via ADO.NET. This feature allows you to parse database tables and extract their content as text, similar to how you would extract data from document files.
API Overview
Constructors
The Parser class provides constructors specifically for database extraction:
// Constructor with DbConnection (recommended for .NET Core/.NET 5+)Parser(DbConnectionconnection);Parser(DbConnectionconnection,ParserSettingsparserSettings);// Constructor with connection string (only for .NET Framework)Parser(stringconnectionString,LoadOptionsloadOptions);
Parameters
Parameter
Type
Description
connection
DbConnection
An active ADO.NET database connection object
connectionString
string
Database connection string (only supported in .NET Framework)
loadOptions
LoadOptions
Must be set to FileFormat.Database when using connection string
parserSettings
ParserSettings
Optional settings for logging and other parser configurations
Return Types
GetToc() returns IEnumerable<TocItem> where each TocItem represents a database table
GetText(int pageIndex) returns TextReader containing the table data for the specified table index
Features.Text and Features.Toc properties indicate if text and table extraction are supported
Supported Database Providers
GroupDocs.Parser works with any ADO.NET-compatible database provider. Common providers include:
Call Features.Text property to check if text extraction is supported;
Call Features.Toc property to check if table of contents extraction is supported;
Call GetToc method and obtain collection of tables;
Iterate through the collection and get a text from tables.
Complete Example: SQLite Database
The following example shows how to extract data from a SQLite database:
usingSystem.Data.Common;usingSystem.Data.SQLite;usingGroupDocs.Parser;usingGroupDocs.Parser.Data;// Connection string for SQLite databasestringdbPath="sample.db";stringconnectionString=$"Data Source={dbPath};Version=3;";// Create DbConnection objectusing(DbConnectionconnection=newSQLiteConnection(connectionString)){connection.Open();// Create an instance of Parser class to extract tables from the databaseusing(Parserparser=newParser(connection)){// Check if text extraction is supportedif(!parser.Features.Text){Console.WriteLine("Text extraction isn't supported for this database.");return;}// Check if table of contents (table list) extraction is supportedif(!parser.Features.Toc){Console.WriteLine("Table of contents extraction isn't supported.");return;}// Get a list of all tables in the databaseIEnumerable<TocItem>tables=parser.GetToc();// Iterate over each tableforeach(TocItemtableintables){Console.WriteLine($"\n=== Table: {table.Text} ===");// Extract table content as text// PageIndex represents the table indexusing(TextReaderreader=parser.GetText(table.PageIndex.Value)){if(reader!=null){stringtableContent=reader.ReadToEnd();Console.WriteLine(tableContent);}else{Console.WriteLine("Unable to extract data from this table.");}}}}}
Example: SQL Server Database
usingSystem.Data.Common;usingSystem.Data.SqlClient;usingGroupDocs.Parser;stringconnectionString="Server=localhost;Database=MyDatabase;Integrated Security=true;";using(DbConnectionconnection=newSqlConnection(connectionString)){connection.Open();using(Parserparser=newParser(connection)){if(parser.Features.Text&&parser.Features.Toc){foreach(TocItemtableinparser.GetToc()){Console.WriteLine($"Extracting from table: {table.Text}");using(TextReaderreader=parser.GetText(table.PageIndex.Value)){if(reader!=null){Console.WriteLine(reader.ReadToEnd());}}}}}}
Extract Data with Connection String
Warning
Important: This functionality is supported only in .NET Framework version of GroupDocs.Parser for .NET. For .NET Core, .NET 5+, or cross-platform applications, use the DbConnection approach described above.
Constructor
To create an instance of Parser class to extract data from a database using a connection string, use:
connectionString: The database connection string (not a file path)
loadOptions: Must be initialized with FileFormat.Database
The list of tables is represented as table of contents (TOC). Each table can be extracted using the GetText(int) method where the integer parameter is the table index from the TOC.
Here are the steps to extract data from Sqlite database:
Call Features.Text property to check if text extraction is supported;
Call Features.Toc property to check if table of contents extraction is supported;
Call GetToc method and obtain collection of tables;
Iterate through the collection and get a text from tables.
Complete Example (.NET Framework Only)
usingGroupDocs.Parser;usingGroupDocs.Parser.Options;stringdatabasePath="sample.db";stringconnectionString=string.Format("Provider=System.Data.Sqlite;Data Source={0};Version=3;",databasePath);// Create an instance of Parser class to extract tables from the database// Pass connection string as first parameter and LoadOptions with FileFormat.Databaseusing(Parserparser=newParser(connectionString,newLoadOptions(FileFormat.Database))){// Check if text extraction is supportedif(!parser.Features.Text){Console.WriteLine("Text extraction isn't supported.");return;}// Check if table of contents extraction is supportedif(!parser.Features.Toc){Console.WriteLine("Table of contents extraction isn't supported.");return;}// Get a list of all tables in the databaseIEnumerable<TocItem>tables=parser.GetToc();// Iterate over tablesforeach(TocItemtableintables){// Print the table nameConsole.WriteLine($"\nTable: {table.Text}");// Extract table content as text using the table's page indexusing(TextReaderreader=parser.GetText(table.PageIndex.Value)){if(reader!=null){Console.WriteLine(reader.ReadToEnd());}}}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Along with full featured .NET library we provide simple, but powerful free Apps.
You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.