Generating C# const declarations from table data

In my previous post I showed how to generate INSERT and UPDATE statements from table data stored in an SQL Server database using dbscript. Now we create a Value Script definition to generate C# classes with const int declarations, one class relating to one database table.

Setting up a Project in dbscript has been explained in the mentioned post.

Now create a Value Script entry with type “C# public consts”. On the Tables page, you find 3 different Script Modes, namely

  • public static const
  • public enum
  • public const

“public const” is to be used with C# 1.x, as it does not support static classes.

After adding tables to the script definition, select each table to define the scripting options:

Class Name

By default, the class is named after the table. If you want to change the class name, enter the desired name.

C# Identifier

The varchar column containing the const identifiers.


The int column containing the values of the C# constants.


Optional column containing a description for each C# constant. Being generated as /// summary comments, the values show up in Intellisense in Visual Studio.

Finally, click Generate in the Value Script’s Action menu. Select the project version the script is based on (a script may contain tables or columns that need not be present in every project version).

Press Generate. The Generated Script textbox will now contain T-SQL statements that generate C# classes and const declarations according to your definition. If you defined a name for a Stored Procedure in the Value Script properties, the script will also contain the CREATE PROCEDURE header.

Alternatively, select a database from the dropdown to retrieve values directly from that database. This is a sample based on the ObjectType table used by dbscript:

// dbscript2dev Value Script
// automatically generated by script
// 2008-12-29 22:07:16
// script generator by dbscript 0.93 29.12.2008 22:07:16

public static class ObjectType
    /// <summary>
    /// Table
    /// </summary>
    public const int Table = 1;
    /// <summary>
    /// View
    /// </summary>
    public const int View = 2;

Copy the generated script or C# code directly from the textbox, or click the download link to save it.

dbscript is available for download here.

3 Responses to Generating C# const declarations from table data

  1. […] Adding to existing functionality creating C# const declarations: […]

  2. […] To keep your code in sync with database values, you can also create C# constants from your database values. […]

  3. […] generates T-SQL value scripts and C# constant declarations based on database […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: