0
Answer

Create Variables and Properties from Database Table

Rajul Aggarwal

Rajul Aggarwal

15y
5.1k
1
Please use the below mentioned queries to generate private variables and public properties that can be used in C# classes.

Note: Please don't forget to change the table name.
---------------------------------------------------------------------------------------------------------

declare @tableName varchar(50)

set @tableName = 'tbTest'

 

SELECT 'private ' + (

            case systypes.name

                  when 'varchar' then 'string'

                  when 'decimal' then 'decimal'

                  when 'datetime' then 'DateTime'

                  when 'int' then 'int'

            else systypes.name end) + ' _' + lower(substring(syscolumns.name, 1, 1)) + right(syscolumns.name, len(syscolumns.name) -1) + ';'

            FROM syscolumns inner join systypes on

      syscolumns.xtype = systypes.xtype

where id = Object_ID(@tableName)

order by colorder

 

SELECT 'public ' + (

            case systypes.name

                  when 'varchar' then 'string'

                  when 'decimal' then 'decimal'

                  when 'datetime' then 'DateTime'

                  when 'int' then 'int'

            else systypes.name end)+ ' ' + syscolumns.name + char(10) +

            '{' + char(10) + 'get { return' +

            ' _' + lower(substring(syscolumns.name, 1, 1)) + right(syscolumns.name, len(syscolumns.name) -1) + '; }'

            + char(10) + 'set { _' + lower(substring(syscolumns.name, 1, 1)) + right(syscolumns.name, len(syscolumns.name) -1) + ' = value; }' + char(10) + '}'

           

            FROM syscolumns inner join systypes on

      syscolumns.xtype = systypes.xtype

where id = Object_ID(@tableName)

order by colorder

------------------------------------------------------------------------------------------------------------------------