Monday, October 10, 2011

SQL Code to generate properties for vb.net

/*
SQL Code to generate properties for vb.net
*/

Declare @TableName varchar(100)
Set @TableName ='TableName'

Declare @Column1 varchar(300),
@Datatype varchar(50),
@ActColumn1 varchar(300),
@FieldType varchar(50)


print '============================================================================================='
print '#Region "Fields" '
DECLARE get_Data CURSOR
FOR SELECT COLUMN_NAME ,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
ORDER BY ORDINAL_POSITION
OPEN get_Data
FETCH NEXT FROM get_Data into @Column1,@Datatype
WHILE @@FETCH_STATUS = 0
BEGIN
set @ActColumn1 = @Column1

if @Datatype = 'int'
Set @FieldType = 'Integer'
else if @Datatype = 'tinyint'
Set @FieldType = 'Int16'
else if @Datatype = 'float'
Set @FieldType = 'Double'
else if @Datatype = 'varchar' or @Datatype = 'text'
Set @FieldType = 'String'
else
Set @FieldType = @Datatype

print 'Private _' + @Column1 + ' as ' + @FieldType
FETCH NEXT FROM get_Data into @Column1,@Datatype
End
CLOSE get_Data
DEALLOCATE get_Data
print ' #End region'
print '============================================================================================='

print '============================================================================================='
print '#Region "Properties" '
DECLARE get_Data CURSOR FOR
SELECT COLUMN_NAME ,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
ORDER BY ORDINAL_POSITION
OPEN get_Data
FETCH NEXT FROM get_Data into @Column1,@Datatype
WHILE @@FETCH_STATUS = 0
BEGIN
set @ActColumn1 = @Column1

if @Datatype = 'int'
Set @FieldType = 'Integer'
else if @Datatype = 'tinyint'
Set @FieldType = 'Int16'
else if @Datatype = 'float'
Set @FieldType = 'Double'
else if @Datatype = 'varchar' or @Datatype = 'text'
Set @FieldType = 'String'
else
Set @FieldType = @Datatype

print 'public property ' + @Column1 + '() as ' + @FieldType
print 'Get'
print 'return _' + @ActColumn1
print ' End Get'
print 'Set(ByVal Value as '+ @FieldType +')'
print '_' + @ActColumn1 + ' = value'
print ' End Set'
print 'End Property'
FETCH NEXT FROM get_Data into @Column1,@Datatype
End
CLOSE get_Data
DEALLOCATE get_Data
print ' #End region'
print '============================================================================================='

print '============================================================================================='
print '#Region "Method" '
print ' Public Function InsertUpdate() As Integer
Dim db As Database = Nothing
Dim dbCommand As DbCommand = Nothing
Try
db = DatabaseFactory.CreateDatabase()
dbCommand = db.GetStoredProcCommand("'+Replace(@TableName,'_','')+'_InsertUpdate")'

DECLARE get_Data CURSOR FOR
SELECT COLUMN_NAME ,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
ORDER BY ORDINAL_POSITION
OPEN get_Data
FETCH NEXT FROM get_Data into @Column1,@Datatype
WHILE @@FETCH_STATUS = 0
BEGIN
set @ActColumn1 = @Column1
if @Datatype = 'int'
Set @FieldType = 'Int32'
else if @Datatype = 'tinyint'
Set @FieldType = 'Int16'
else if @Datatype = 'float'
Set @FieldType = 'Double'
else if @Datatype = 'varchar' or @Datatype = 'text'
Set @FieldType = 'String'
else
Set @FieldType = @Datatype

print ' db.AddInParameter(dbCommand, "@'+@Column1+'", DbType.'+@FieldType+', _'+@Column1+')'
FETCH NEXT FROM get_Data into @Column1,@Datatype
End
CLOSE get_Data
DEALLOCATE get_Data
print ''
print ' Dim returnValue As Integer = Convert.ToInt32(db.ExecuteScalar(dbCommand))
Return returnValue
Finally
BObase.DisposeOf(dbCommand)
BObase.DisposeOf(db)
End Try
End Function'


print 'Public Function View() As Boolean'
print ' Dim dt As DataTable = Me.SelectById()'
print ' If Not BObase.IsNothingOrEmpty(dt) Then'
print ' Dim dr As DataRow = dt.Rows(0)'
print ' BObase.SetDefaultValuesForDBNull(dr)'
print ' With Me'

DECLARE get_Data CURSOR
FOR SELECT COLUMN_NAME ,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
ORDER BY ORDINAL_POSITION
OPEN get_Data
FETCH NEXT FROM get_Data into @Column1,@Datatype
WHILE @@FETCH_STATUS = 0
BEGIN

print ' ._' + @Column1 + ' = dr("' + @Column1 + '")'
FETCH NEXT FROM get_Data into @Column1,@Datatype
End
CLOSE get_Data
DEALLOCATE get_Data
print ' End With'
print ' Return True'
print ' Else'
print ' Return False'
print ' End If'
print 'End Function'
print ' #End region'
print '============================================================================================='




print ' With objProperties'
DECLARE get_Data CURSOR
FOR SELECT COLUMN_NAME ,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
ORDER BY ORDINAL_POSITION
OPEN get_Data
FETCH NEXT FROM get_Data into @Column1,@Datatype
WHILE @@FETCH_STATUS = 0
BEGIN
set @ActColumn1 = @Column1
If @Datatype='int'
begin
set @Datatype='Integer'
set @ActColumn1= substring(@Column1,0,len(@Column1)+1)
set @Column1='lst' + substring(@Column1,0,len(@Column1)+1) + '.SelectedValue'
end
else if @Datatype='nvarchar'
begin
set @Datatype='String'
set @ActColumn1= substring(@Column1,0,len(@Column1)+1)
set @Column1='txt' + substring(@Column1,0,len(@Column1)+1) +'.text'
end
else if @Datatype='datetime'
begin
set @Datatype='String'
set @ActColumn1= substring(@Column1,0,len(@Column1)+1)
set @Column1='txt' + substring(@Column1,0,len(@Column1)+1) +'.text'
end
else if @Datatype='bit'
begin
set @Datatype='Boolean'
set @ActColumn1= substring(@Column1,0,len(@Column1)+1)
set @Column1='chk' + substring(@Column1,0,len(@Column1)+1) + '.checked'
end
else if @Datatype='decimal'
begin
set @Datatype='Double'
set @ActColumn1=substring(@Column1,0,len(@Column1)+1)
set @Column1='txt' +substring(@Column1,0,len(@Column1)+1) +'.text'
end
else if @Datatype='float'
begin
set @Datatype='Double'
set @ActColumn1=substring(@Column1,0,len(@Column1)+1)
set @Column1='txt' +substring(@Column1,0,len(@Column1)+1) +'.text'
end

print ' ' + @Column1 + ' = .' + @ActColumn1

FETCH NEXT FROM get_Data into @Column1,@Datatype
End
CLOSE get_Data
DEALLOCATE get_Data
print ' End With'
print '============================================================================================='
print ' With objProperties'
DECLARE get_Data CURSOR
FOR SELECT COLUMN_NAME ,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
ORDER BY ORDINAL_POSITION
OPEN get_Data
FETCH NEXT FROM get_Data into @Column1,@Datatype
WHILE @@FETCH_STATUS = 0
BEGIN
set @ActColumn1 = @Column1
If @Datatype='int'
begin
set @Datatype='Integer'
set @ActColumn1= substring(@Column1,0,len(@Column1)+1)
set @Column1='lst' + substring(@Column1,0,len(@Column1)+1) + '.SelectedValue'
end
else if @Datatype='nvarchar'
begin
set @Datatype='String'
set @ActColumn1= substring(@Column1,0,len(@Column1)+1)
set @Column1='txt' + substring(@Column1,0,len(@Column1)+1) +'.text'
end
else if @Datatype='datetime'
begin
set @Datatype='String'
set @ActColumn1= substring(@Column1,0,len(@Column1)+1)
set @Column1='txt' + substring(@Column1,0,len(@Column1)+1) +'.text'
end
else if @Datatype='bit'
begin
set @Datatype='Boolean'
set @ActColumn1= substring(@Column1,0,len(@Column1)+1)
set @Column1='chk' + substring(@Column1,0,len(@Column1)+1) + '.checked'
end
else if @Datatype='decimal'
begin
set @Datatype='Double'
set @ActColumn1=substring(@Column1,0,len(@Column1)+1)
set @Column1='txt' +substring(@Column1,0,len(@Column1)+1) +'.text'
end
else if @Datatype='float'
begin
set @Datatype='Double'
set @ActColumn1=substring(@Column1,0,len(@Column1)+1)
set @Column1='txt' +substring(@Column1,0,len(@Column1)+1) +'.text'
end

print ' .' + @ActColumn1 + ' = ' + @Column1

FETCH NEXT FROM get_Data into @Column1,@Datatype
End
CLOSE get_Data
DEALLOCATE get_Data
print ' End With'
print '============================================================================================='

print '============================================================================================='
print '#Region "Fields" '

DECLARE @strOutput varchar(Max)
SET @strOutput = 'public sub New ('
DECLARE get_Data CURSOR
FOR SELECT COLUMN_NAME ,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
ORDER BY ORDINAL_POSITION
OPEN get_Data
FETCH NEXT FROM get_Data into @Column1,@Datatype
WHILE @@FETCH_STATUS = 0
BEGIN
set @ActColumn1 = @Column1

if @Datatype = 'int'
Set @FieldType = 'Integer'
else if @Datatype = 'tinyint'
Set @FieldType = 'Int16'
else if @Datatype = 'float'
Set @FieldType = 'Double'
else if @Datatype = 'varchar' or @Datatype = 'text'
Set @FieldType = 'String'
else
Set @FieldType = @Datatype

SET @strOutput = @strOutput + @Column1 + ' as ' + @FieldType + ', '
FETCH NEXT FROM get_Data into @Column1,@Datatype
End
CLOSE get_Data
DEALLOCATE get_Data
PRINT @strOutput
print ' #End region'
print '============================================================================================='

print '============================================================================================='
print '#Region "Fields" '
DECLARE get_Data CURSOR
FOR SELECT COLUMN_NAME ,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
ORDER BY ORDINAL_POSITION
OPEN get_Data
FETCH NEXT FROM get_Data into @Column1,@Datatype
WHILE @@FETCH_STATUS = 0
BEGIN
set @ActColumn1 = @Column1

if @Datatype = 'int'
Set @FieldType = 'Integer'
else if @Datatype = 'tinyint'
Set @FieldType = 'Int16'
else if @Datatype = 'float'
Set @FieldType = 'Double'
else if @Datatype = 'varchar' or @Datatype = 'text'
Set @FieldType = 'String'
else
Set @FieldType = @Datatype

PRINT 'Me.'+ @Column1 + ' = ' + @Column1
FETCH NEXT FROM get_Data into @Column1,@Datatype
End
CLOSE get_Data
DEALLOCATE get_Data
print ' #End region'
print '============================================================================================='

No comments: