MySQLToCsharp 1.4.0

dotnet tool install --global MySQLToCsharp --version 1.4.0
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local MySQLToCsharp --version 1.4.0
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=MySQLToCsharp&version=1.4.0
                    
nuke :add-package MySQLToCsharp --version 1.4.0
                    

dotnet-build NuGet

MySQLToCsharp

MySQL version of SqlToCsharp.

A C# class generator from SQL CREATE TABLE Statements (MySQLs)

Install

dotnet tool install --global MySQLToCsharp

Sample

Open MySQLToCsharp.sln and set following.

MySQLToCsharp project > Properties > Debug > Application arguments

dir -i ../../../../../samples/tables -o ../../../../../samples/MySQLToCsharpSampleConsoleApp -n MySQLToCsharpSampleConsoleApp

Debug Run MySQLToCsharp.

Output Directory: ../../../../../samples/MySQLToCsharpSampleConsoleApp
[-] skipped: BinaryData.cs (no change)
[-] skipped: Character.cs (no change)
[-] skipped: CharacterSlot.cs (no change)
[-] skipped: Multi.cs (no change)
[-] skipped: Player.cs (no change)
[-] skipped: Room.cs (no change)
[-] skipped: String.cs (no change)
[-] skipped: Weapon.cs (no change)

change Converter to StandardDateTimeAsOffsetConverter and Debug Run.

dir -i ../../../../../samples/tables -o ../../../../../samples/MySQLToCsharpSampleConsoleApp -n MySQLToCsharpSampleConsoleApp -c StandardDateTimeAsOffsetConverter

This change CharacterSlot.cs as MySQL DATETIME will convert to C# DateTimeOffset.

Output Directory: ../../../../../samples/MySQLToCsharpSampleConsoleApp
[-] skipped: BinaryData.cs (no change)
[-] skipped: Character.cs (no change)
[o] generate: CharacterSlot.cs
[-] skipped: Multi.cs (no change)
[-] skipped: Player.cs (no change)
[-] skipped: Room.cs (no change)
[-] skipped: String.cs (no change)
[-] skipped: Weapon.cs (no change)

How to run

There are 3 options to generate C# code from MySQL Create Table query.

  1. query: input sql string and generate a class.
  2. file: read sql file and generate a class.
  3. dir: read directory path and generate class for each *.sql file.
$ MySQLToCsharp --help

Usage: MySQLToCsharp <Command>

Commands:
  query    Convert DDL sql query and generate C# class.
  file     Convert DDL sql file and generate C# class.
  dir      Convert DDL sql files in the folder and generate C# class.

query

help.

$ MySQLToCsharp query --help

Usage: MySQLToCsharp query [options...]

Convert DDL sql query and generate C# class.

Options:
  -i, -input <String>        input mysql ddl query to parse (Required)
  -o, -output <String>       output directory path of generated C# class file (Required)
  -n, -namespace <String>    namespace to write (Required)
  -c, -converter <String>    converter name to use (Default: StandardConverter)
  -addbom <Boolean>           (Default: False)
  -dry <Boolean>              (Default: False)

sample

dotnet mysql2csharp query -i "CREATE TABLE sercol1 (id INT, val INT);" -o bin/out -n MyNameSpace.Data

file

help.

$ MySQLToCsharp file --help

Usage: MySQLToCsharp file [options...]

Convert DDL sql file and generate C# class.

Options:
  -i, -input <String>        input file path to parse mysql ddl query (Required)
  -o, -output <String>       output directory path of generated C# class file (Required)
  -n, -namespace <String>    namespace to write (Required)
  -c, -converter <String>    converter name to use (Default: StandardConverter)
  -addbom <Boolean>           (Default: False)
  -dry <Boolean>              (Default: False)

sample

dotnet mysql2csharp file -i "./MySQLToCsharp.Tests/test_data/sql/create_table.sql" -o bin/out -n MyNameSpace.Data

dir

help.

$ MySQLToCsharp dir --help

Usage: MySQLToCsharp dir [options...]

Convert DDL sql files in the folder and generate C# class.

Options:
  -i, -input <String>        input folder path to parse mysql ddl query (Required)
  -o, -output <String>       output directory path of generated C# class files (Required)
  -n, -namespace <String>    namespace to write (Required)
  -c, -converter <String>    converter name to use (Default: StandardConverter)
  -addbom <Boolean>           (Default: False)
  -dry <Boolean>              (Default: False)

sample

dotnet mysql2csharp dir -i "./MySQLToCsharp.Tests/test_data/sql/" -o bin/out -n MyNameSpace.Data

Available Conveters

  • StandardConverter
  • StandardBitAsBoolConverter
  • StandardDateTimeAsOffsetConverter

Help

$ dotnet mysql2csharp query --help
$ dotnet mysql2csharp dir --help
$ dotnet mysql2csharp file --help

Generate MySQL Lexer/Parser/Listener/Visitor from ANTLR4 grammer

Referencing MySQL ANTLR4 Grammer from antlr/grammars-v4.

Follow step to update lexer and parser.

  1. Update MySqlLexer.g4 and MySqlParser.g4 to the latest.
  2. Run script to generate C# class files.
  • it calls docker compose up and generate lexer, parser, listener and visitor class.
  1. Run Build and Test and confirm what changed and actual effect.
# windows
gen.bat

# macos/linux
gen.sh

Ref

ANTLR4 Getting started

ANTLR4 samples

MSSQL Parser reference

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last Updated
1.4.0 179 10/14/2025
1.3.0 881 11/18/2020
1.2.0 690 7/5/2020
1.1.1 2,521 2/20/2020
1.1.0 709 2/20/2020
1.0.1 751 2/20/2020
1.0.0 3,942 1/23/2020
0.4.0 761 1/23/2020
0.3.0 663 10/25/2019
0.2.0 768 8/16/2019
0.1.0 797 8/2/2019