Skip to content

SQL Parser for .NET and C#

Use General SQL Parser (GSP) when your .NET application needs to understand SQL as structured data instead of treating it as a string. The gudusoft.gsqlparser NuGet package parses SQL inside your process, exposes its abstract syntax tree (AST), and lets your code validate, inspect, analyze, format, or rewrite the statement without sending it to a database.

Open Quick Start Clone demos

The Quick Start is the step-by-step installation and first program tutorial; select C# once and the paired examples follow that choice. This page helps you decide what to build next and takes you directly to the corresponding C# guide and source project.

Choose a .NET example by task

The How-to guide index organizes the same features by the problem you need to solve. Each completed guide provides Java and C# tabs and links to runnable source code.

Where GSP fits in a .NET application

A typical integration has five steps:

1
2
3
4
5
6
7
8
9
SQL from a user, file, API, model, or repository
Choose the real database dialect
Parse locally and inspect diagnostics or AST nodes
Validate, extract, analyze, format, or modify
Return a decision, metadata, or regenerated SQL

Because parsing happens in process, the same workflow can run in a desktop editor, ASP.NET service, build agent, migration utility, or air-gapped environment. A live database is only needed if your own application also wants to execute the SQL or obtain external catalog metadata.

This separation is useful in several common designs:

  • Pre-execution safety layer: inspect submitted SQL before a database driver receives it, then reject it or apply an approved rewrite.
  • Developer tooling: show dialect-aware syntax errors, formatting, table references, or AST details inside an editor or pull-request check.
  • Data platform ingestion: validate scripts at the boundary of an ETL, orchestration, or metadata pipeline so malformed input does not travel downstream.
  • SQL migration: locate vendor-specific structures and transform them using syntax-aware nodes instead of fragile text replacement.
  • AI and text-to-SQL controls: parse model output, inspect what it would access, enforce application policy, and only then decide whether it can be executed.

GSP performs syntactic parsing and exposes structural information. It does not replace database permissions, transaction controls, parameter binding, or a database optimizer; production safety normally combines those controls with the parser-based layer.

Clone and run the demo suite

The gsp_demo_dotnet repository contains independent console projects for syntax checking, formatting, table and column extraction, AST traversal, SQL rewriting, join conversion, and data lineage.

1
2
3
git clone https://github.com/sqlparser/gsp_demo_dotnet.git
cd gsp_demo_dotnet
dotnet build gsp_demo_dotnet.slnx -c Release

Run the offline syntax checker against the included SQL file:

1
2
dotnet run --project src/demos/checksyntax/demos.checksyntax.csproj \
  -c Release -- /f samples/checksyntax/valid-mssql.sql /t mssql

Each demo is a small project you can run first and then copy or adapt inside your own solution. The repository README lists every available project, accepted dialect argument, sample file, and test command.

Supported .NET targets and SQL dialects

The NuGet package provides net10.0 and netstandard2.0 assemblies. It can be used from modern .NET on Windows, Linux, and macOS, and from .NET Framework 4.6.2 or newer through the netstandard2.0 assembly. See the framework compatibility details before choosing a target framework for an existing application.

The .NET build includes 15 dedicated grammars:

  • SQL Server, Oracle, PostgreSQL, MySQL, and DB2
  • Snowflake, Redshift, Teradata, Greenplum, and Netezza
  • Hive, Impala, Informix, Sybase, and MDX

Always choose the dialect that matches the SQL you actually receive. For example, SQL Server TOP, PostgreSQL :: casts, Snowflake QUALIFY, and Oracle (+) joins should be parsed with their corresponding vendor setting. The listGSPInfo demo prints the version, edition, and dialects available in the package your project resolved.

Reading Java and C# examples together

The Java and .NET editions share the same parser concepts, AST structure, and many class names. That makes the Java/C# tabs throughout this site easy to compare, but the APIs and dialect lists are not identical. Use the C# tab when copying code into a .NET project.

Java C#
parser.setSqltext(sql) / parser.sqltext = sql parser.sqltext = sql
parser.parse() parser.parse()
parser.getErrormessage() parser.Errormessage
parser.getSyntaxErrors() parser.SyntaxErrors
stmt.getWhereClause() stmt.WhereClause
select.getResultColumnList() select.ResultColumnList
stmt.getStatements() stmt.Statements

Java getters usually become C# properties, while GSP list objects retain the size() and get(i) method style. The AST node reference explains the objects you will encounter after parsing.

Package and production use

Install the public package with:

1
dotnet add package gudusoft.gsqlparser

The package on NuGet is the trial edition. It is suitable for running the examples, but it enforces input-size and evaluation-period limits. Review the trial behavior in the Quick Start and the licensing FAQ before planning a production deployment. For an environment without internet access, follow the offline NuGet feed instructions.

.NET SQL parser FAQ

Does GSP need a database connection to parse SQL?

No. Core parsing, validation, AST inspection, formatting, and rewriting run locally inside the .NET process. Your application may supply schema metadata for more context, but it does not need a live database merely to parse SQL.

Can one C# service parse SQL from different databases?

Yes. Create the parser with the matching vendor for each input. This is useful for migration tools, metadata services, and platforms that accept SQL from multiple database products. Do not parse every input as a generic dialect; vendor selection is what gives the parser the correct grammar.

Can GSP change SQL as well as read it?

Yes. Your code can modify supported AST nodes and regenerate SQL. Reparse the generated statement before execution so the rewrite stage has its own validation boundary. The AST modification guide shows that complete workflow.

Where is the C# getting-started tutorial?

Open the shared Quick Start and select the C# tab. It covers package installation, the first parser program, vendor selection, syntax errors, table extraction, formatting, trial limits, and troubleshooting. Your language choice follows you across other paired examples on this site.

Where can I ask an integration question?

Use the support options for product help, or open an issue in gsp_demo_dotnet when a question is specifically about one of the public demo projects.