Technical Article

Procedure Class Generator

,

  1. First you must select your database.
  2. Change the @proc variable to your Stored Procedure Name like "spGetCustomerData".
  3. Execute

Of course you can take this script and create a procedure that takes a parameter if you like. Does this do all the work for you? No. You still need to populate the class with whatever data access object you are using, but for me it is a good starting point.

declare @proc varchar(255)
declare @type varchar(255)
declare @pname varchar(255)
declare @len integer
declare @typid integer
declare @netType varchar(255)
declare @netPrivName varchar(255)
declare @netPropName varchar(255)
declare @dta varchar(800)
set @proc = 'Put Your Procedure Name here' 

declare p_curs cursor for 
select p.[name], p.max_length, p.system_type_id, t.[name]
 from sys.all_parameters p
 inner join sys.types t on p.system_type_id = t.system_type_id
 where [object_id] = object_id(@proc)
 order by parameter_id

declare @class table ( dta varchar(800), row integer identity(1,1) )
declare @members table ( dta varchar(800), row integer identity(1,1) )
declare @props table ( dta varchar(800), row integer identity(1,1) )

insert into @class ( dta ) values ( 'Public Class ' + @proc )

open p_curs
fetch next from p_curs into @pname, @len, @typid, @type
while @@fetch_status = 0
begin
 set @netType = case when @typid = 35 then 'String'
 when @typid = 36 then 'String'
 when @typid = 48 then 'Integer'
 when @typid = 52 then 'Integer'
 when @typid = 56 then 'Integer'
 when @typid = 58 then 'DateTime'
 when @typid = 60 then 'Double'
 when @typid = 61 then 'DateTime'
 when @typid = 62 then 'Double'
 when @typid = 99 then 'String'
 when @typid = 104 then 'Integer'
 when @typid = 106 then 'Double'
 when @typid = 108 then 'Double'
 when @typid = 122 then 'Double'
 when @typid = 127 then 'Long'
 when @typid = 167 then 'String'
 when @typid = 175 then 'String'
 when @typid = 231 then 'String'
 when @typid = 239 then 'String'
 when @typid = 241 then 'String'
 end

 set @netPrivName = replace(@pname, '@', '_')
 set @netPropName = replace(@pname, '@', '')
 set @dta = ' Private ' + @netPrivName + ' as ' + @netType
 insert into @members ( dta ) values ( @dta )
 set @dta = ' Public Property ' + @netPropName + '() as ' + @netType
 insert into @props ( dta ) values ( @dta )
 set @dta = ' Get'
 insert into @props ( dta ) values ( @dta )
 set @dta = ' Return ' + @netPrivName
 insert into @props ( dta ) values ( @dta )
 set @dta = ' End Get'
 insert into @props ( dta ) values ( @dta )
 set @dta = ' Set(ByVal value as ' + @netType + ')'
 insert into @props ( dta ) values ( @dta )
 set @dta = ' ' + @netPrivName + ' = value'
 insert into @props ( dta ) values ( @dta )
 set @dta = ' End Set'
 insert into @props ( dta ) values ( @dta )
 set @dta = ' End Property'
 insert into @props ( dta ) values ( @dta )

 fetch next from p_curs into @pname, @len, @typid, @type
end
close p_curs
deallocate p_curs

insert into @class ( dta )
select dta from @members order by row
insert into @class ( dta )
select dta from @props order by row
insert into @class
select 'End Class'

select dta from @class order by row

Rate

2.67 (3)

You rated this post out of 5. Change rating

Share

Share

Rate

2.67 (3)

You rated this post out of 5. Change rating