Table of Contents
Install General SQL Parser package from www.nuget.org and once installed, refer to the package in code with using gudusoft.gsqlparser. You can then use the package’s API.
Prerequisites
- The .NET Core SDK, which provides the command-line dotnet tool.
Create a project
General SQL Parser packages can be installed into a .NET project of some kind. For this walkthrough, create a simple .NET Core console project as follows:
- Create a folder for the project.
-
Create the project using the following command:
dotnet new console
-
Use dotnet run to test that the app has been created properly.
dotnet run
Add the gudusoft.gsqlparser NuGet package
-
Use the following command to install the gudusoft.gsqlparser package:
dotnet add package gudusoft.gsqlparser
-
After the command completes, open the .csproj file to see the added reference:
<ItemGroup> <PackageReference Include="gudusoft.gsqlparser" Version="3.2.6.5" /> </ItemGroup>
Use the gudusoft.gsqlparser API in the app
-
Open the Program.cs file and add the following line at the top of the file:
using gudusoft.gsqlparser;
-
Replace the Main function with the following:
static void Main(string[] args) { string sqlText = @"SELECT e.last_name AS name, e.commission_pct comm, e.salary * 12 ""Annual Salary"" FROM scott.employees AS e WHERE e.salary > 1000 or 1=1 ORDER BY e.first_name, e.last_name;"; TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle); Console.WriteLine("versionId: "+ TBaseType.versionId); Console.WriteLine("releaseDate: "+TBaseType.releaseDate); sqlparser.sqltext = sqlText; int ret = sqlparser.parse(); if (ret == 0) { Console.WriteLine("Congratulations, you have successfully setup the general SQL parser."); } else { Console.WriteLine("Syntax error detected: {0}",sqlparser.Errormessage); } }
-
Build and run the app by using the command
dotnet run
The output should be something like this:
Congratulations, you have successfully setup the general SQL parser
.