Technical Article

Create property methods for fields using query

,

Probably, many of you know this trick, but I thought it might be useful to people who doesn't know it. This script, when run in query analyzer with text output selected, simply creates property methods code. This example is written for creating property methods for c#. But you can modify it for any language. The only benefit of this script is that it saves a lot of time, when you have to write property methods for a class that has too many properties (fields). You have to add cases for all the data types that your table uses.

SET NOCOUNT ON
select 
'/// <summary>
/// Retrieves ' + column_name + ' from Author object
/// </summary>
internal static ' +
case when data_type='varchar' then 'String '
when data_type='char' then 'String '
when data_type='tinyint' then 'Byte '
when data_type='money' then 'Decimal '
end

+ column_name + '
{
get
{
' +
case when data_type='varchar' then 'String str'
when data_type='char' then 'String str'
when data_type='tinyint' then 'Byte by'
when data_type='money' then 'Decimal dec'
end + column_name + '=(' +

case when data_type='varchar' then 'String'
when data_type='char' then 'String'
when data_type='tinyint' then 'Byte'
when data_type='money' then 'Decimal'
end

+ ')GetAuthorValue("' +
upper(column_name)
+ '","' +  
case when data_type='varchar' then 'String'
when data_type='char' then 'String'
when data_type='tinyint' then 'Byte'
when data_type='money' then 'Decimal'
end+   
'");
return ' + 
case when data_type='varchar' then 'str'
when data_type='char' then 'str'
when data_type='tinyint' then 'by'
when data_type='money' then 'dec'
end + column_name +
';'
+ '
}
}// End of Property ' + column_name + '

'
from information_schema.columns where table_name='Authors'
order by ordinal_position

SET NOCOUNT OFF

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating