SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL snippet: table to .Net class generator


TSQL snippet: table to .Net class generator

Author
Message
Lambert Antonio
Lambert Antonio
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 38
Heres a snippet for generating a VB.net class from a table, just replace the target table name

just a few tweaks and you can generate one for C# Hehe

cheers!


declare @field table (fieldname varchar(50), fieldtype varchar(50))
declare @table_name varchar(50)
set @table_name = 'TargetTable' -- this is the table name

insert into @field
Select sc.name, st.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

print 'Public Class ' + @table_name

declare @fieldname varchar(50)
declare @fieldtype varchar(50)
declare @constructor varchar(500)
declare @assign varchar(500)

declare cur cursor for
select fieldname, fieldtype
from @field

open cur

fetch next from cur
into @fieldname, @fieldtype

set @constructor = ''
set @assign = ''

while @@fetch_status = 0
begin
set @fieldtype = case @fieldtype
when 'int' then 'Int32'
when 'tinyint' then 'Int32'
when 'varchar' then 'String'
when 'bit' then 'Boolean'
when 'float' then 'Double'
when 'datetime' then 'Datetime'
else @fieldtype
end

set @constructor = @constructor + 'ByVal v' + @fieldname + ' AS ' + @fieldtype + ', '
set @assign = @assign + '_' + @fieldname + ' = v' + @fieldname + ' : '

print ''
print 'Private _' + @fieldname + ' AS ' + @fieldtype
print 'Public Property ' + @fieldname + '() AS ' + @fieldtype
print ' Get'
print ' return _' + @fieldname
print ' End Get'
print ' Set (value AS ' + @fieldtype + ')'
print ' _' + @fieldname + ' = value'
print ' End Set'
print 'End Property'
print ''

fetch next from cur
into @fieldname, @fieldtype
end

print 'Public Sub New(' + substring(@constructor, 0, len(@constructor)) + ')'
print ' ' + substring(@assign, 0, len(@assign) -1)
print 'End Sub'

print 'End Class'





slow down when you need to hurry, stop when you need to move on,
look back when you need to forget, or you might slip and leave sanity
Ramil Joaquin
Ramil Joaquin
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 7
nice article... tnx
Antares686
Antares686
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: Moderators
Points: 25314 Visits: 785
Make sure you post this to the site script library and note it a bit better. As well you should do one for C# to ensure your work is credited.



RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32984 Visits: 9518
Here is a vesion without any Cursors:

create proc spVB_Make_Class as
--from Lambert Antonio, 26-jun-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 = 'TargetTable' -- this is the table name

insert into @field
Select sc.name
, case st.name
when 'int' then 'Int32'
when 'tinyint' then 'Int32'
when 'varchar' then 'String'
when 'bit' then 'Boolean'
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 @VB 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 @VB(Line) Select 'Public Class ' + @table_name

Insert into @VB(Line)
Select '
Private _' + fieldname + ' AS ' + fieldtype + '
Public Property ' + fieldname + '() AS ' + fieldtype + '
Get
return _' + fieldname + '
End Get
Set (value AS ' + fieldtype + ')
_' + fieldname + ' = value
End Set
End Property
'
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 @VB(Line) Select ' Public Sub New('
Insert into @VB(Line) Select ' ' + Line From @constructor Order By ID
Insert into @VB(Line) Select ' )'
Insert into @VB(Line) Select ' ' + Line From @assign Order By ID
Insert into @VB(Line) Select '
End Sub

End Class'

Select Line From @VB Order By ID



-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
david.kelly-684973
david.kelly-684973
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 Visits: 330
This script is greatBigGrinBigGrin 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




oiuoiu
oiuoiu
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 0
Oh my god, I love you!
ashkiamay
ashkiamay
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 0
Awesome!! Thank-you so much. Staring at Linq all day was giving me a headache.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search