Home Forums Programming General TSQL snippet: table to .Net class generator RE: TSQL snippet: table to .Net class generator

  • This script is great:D:D I'm beside myself with joy

    Below is my first attempt at posting here or anywhere on the net so be kind.

    I've converted the vb code to csharpe and it works for me. Thanks to all for their involvment in doing these things that help others.

    David Kelly

    SQL novice

    C# programmer novice

    General old guy

    create proc spCSharpe_Make_Class as

    --from Lambert Antonio, 26-jun-2008

    --converted to C# David Kelly, 06/27/2008

    Set NoCount ON

    declare @field table (id int identity primary key clustered, fieldname varchar(50), fieldtype varchar(50))

    declare @table_name varchar(50)

    set @table_name = 'JBMTest' -- this is the table name

    insert into @field

    Select sc.name

    , case st.name

    --when 'int' then 'Int32'

    when 'money' then 'double'

    when 'tinyint' then 'int'

    when 'varchar' then 'string'

    when 'bit' then 'bool'

    when 'float' then 'double'

    when 'datetime' then 'DateTime'

    else st.name

    end As [name]

    from

    sysobjects so,

    syscolumns sc,

    systypes st

    where

    so.id = sc.id

    and so.xtype = 'U'

    and so.name = @table_name

    and st.xtype = sc.xtype

    declare @CSharpe table (id int identity primary key clustered, Line varchar(8000))

    declare @constructor table (id int identity(1000000,1) primary key clustered, Line varchar(8000))

    declare @assign table (id int identity(1000000,1) primary key clustered, Line varchar(8000))

    Insert into @CSharpe(Line) Select 'using System'

    Insert into @CSharpe(Line) Select 'using System.Collections.Generic'

    Insert into @CSharpe(Line) Select 'using System.Text';

    Insert into @CSharpe(Line) Select ''; -- blank line

    Insert into @CSharpe(Line) Select 'namespace MCL' -- Namespace

    Insert into @CSharpe(Line) Select '{' -- Namespace

    Insert into @CSharpe(Line) Select ' class ' + @table_name -- Class definition

    Insert into @CSharpe(Line) Select ' {' -- Class

    Insert into @CSharpe(Line)

    Select '

    private ' + fieldtype + ' m_' +fieldtype + fieldname + ';

    public ' + fieldtype + ' prop'+ fieldname + '

    {

    get { return m_' + fieldname + '; }

    set {

    m_' +fieldtype+ fieldname + ' = value;

    }

    }

    '

    From @field

    Order By ID

    Insert into @constructor(Line)

    Select 'ByVal v' + fieldname + ' AS ' + fieldtype + ', '

    From @field

    Order By ID

    Insert into @assign(Line)

    Select '_' + fieldname + ' = v' + fieldname

    From @field

    Order By ID

    Insert into @CSharpe(Line) Select ' public '+ @table_name+'()'

    Insert into @CSharpe(Line) Select ' {'

    Insert into @CSharpe(Line) Select ' }'

    Insert into @CSharpe(Line) Select ' } // do not go below this line' -- End Class'

    Insert into @CSharpe(Line) Select '}' -- End namespace

    Select Line From @CSharpe Order By ID