Thursday, March 8, 2012

Creating property get and set clauses based on a table or sp

Is there a way to automatically (wizard?) to create the get and set clauses of a class based on a SQL table or sp? I need to wrap a number of tables and sp into classes to use them in an ASP.net application and it's rather tedious to do type each one.

What I would like is something that I could point at a table and have it create a class and the get and set properties. I could save that as a class and use it in the program.
eg. Point to a table people.
Results would be
Class People
property FirstName()
get

set

...

Aim.You can create a strong typed data set.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcongeneratingstronglytypeddataset.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconusingannotationswithtypeddataset.asp

It will generate the class based on a xml schema, and you can easily build a schema by drag and dropping your table on the xml scheman designer.|||Thanks for the reply, I appreciate your time. However, I am not sure that it is what I want. Maybe I am missing the point.

I want to create a class based on a table. The class would have a local variable and a property get/set clause for each field in the table.
eg if the table were
FirstName
LastName
Middle Initial

I would end up with a class that looked something like

class Name
dim fname as string
dim lname as string
dim mi as string

Public Property FirstName() As String
Get
Return fname
End Get
Set(ByVal Value As String)
fname = Value
End Set
End Property

Public Property LastName() As String
Get
Return lname
End Get
Set(ByVal Value As String)
lname = Value
End Set
End Property
Public Property MiddleInitial() As String
Get
Return mi
End Get
Set(ByVal Value As String)
mi = Value
End Set
End Property

end class

If anyone knows how I can do this I would appreciate a hint or two.

Aim.|||You can do it yourself if you set your query analyzer output to text mode with space separators.


declare @.TABLE_NAME='Name'

print 'Class ' + @.TABLE_NAME + '
'

select 'dim _'+COLUMN_NAME+' as'
, CASE DATA_TYPE WHEN 'int' THEN 'Integer' ELSE 'string' END
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME=@.TABLE_NAME

select 'Public Property '+COLUMN_NAME+' As',
CASE DATA_TYPE WHEN 'int' THEN 'Integer' ELSE 'String' END, '
Get
Return _'+COLUMN_NAME+'
End Get
Set(ByVal Value As',
CASE DATA_TYPE WHEN 'int' THEN 'Integer' ELSE 'String' END,
')
_'+COLUMN_NAME+' = Value
End Set
End Property

'
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME=@.TABLE_NAME

print 'End Class'

|||Thanks for that. It's a cute solution. Maybe I can package that and make my own wizard.

Cheers

Aim.

No comments:

Post a Comment