Generate Properties from Database Table
-- =============================================
-- Author: Duminda Piumwardena
-- Create date: 10/10/2012
-- Description: To generate class properties from tables
-- =============================================
Alter PROCEDURE GenerateProperties
@tableName varchar(50)
AS
BEGIN
DECLARE @LINEFEED varchar(10)
SET @LINEFEED = CHAR(13)+CHAR(10)
SELECT '#region Fields'+@LINEFEED
UNION ALL
SELECT 'private ' +
CASE data_type
WHEN 'bigint' THEN 'int'
-- WHEN 'binary' THEN 'raw'
WHEN 'bit' THEN 'bool'
WHEN 'char' THEN 'char'
WHEN 'datetime' THEN 'DateTime'
WHEN 'decimal' THEN 'decimal'
WHEN 'float' THEN 'float'
-- WHEN 'image' THEN 'long raw'
WHEN 'int' THEN 'int'
WHEN 'money' THEN 'decimal'
WHEN 'nchar' THEN 'string'
WHEN 'ntext' THEN 'string'
WHEN 'nvarchar' THEN 'string'
WHEN 'numeric' THEN 'decimal'
WHEN 'real' THEN 'decimal'
WHEN 'smalldatetime' THEN 'DateTime'
WHEN 'smallmoney' THEN 'double'
WHEN 'smallint' THEN 'int'
WHEN 'text' THEN 'string'
WHEN 'timestamp' THEN 'Date'
WHEN 'uniqueidentifier' THEN 'GUID'
--WHEN 'varbinary' THEN 'raw'
WHEN 'varchar' THEN 'string'
ELSE 'UNIDENTIFIED DataType ' + data_type + ''
END + ' _' + COLUMN_NAME + ';'
FROM information_schema.columns
WHERE table_name = @tableName
UNION ALL
SELECT @LINEFEED+'#endregion'+@LINEFEED
UNION ALL
SELECT '#region Properties'+@LINEFEED
UNION ALL
SELECT 'public ' +
CASE data_type
WHEN 'bigint' THEN 'int'
-- WHEN 'binary' THEN 'raw'
WHEN 'bit' THEN 'bool'
WHEN 'char' THEN 'char'
WHEN 'datetime' THEN 'DateTime'
WHEN 'decimal' THEN 'decimal'
WHEN 'float' THEN 'float'
-- WHEN 'image' THEN 'long raw'
WHEN 'int' THEN 'int'
WHEN 'money' THEN 'decimal'
WHEN 'nchar' THEN 'string'
WHEN 'ntext' THEN 'string'
WHEN 'nvarchar' THEN 'string'
WHEN 'numeric' THEN 'decimal'
WHEN 'real' THEN 'decimal'
WHEN 'smalldatetime' THEN 'DateTime'
WHEN 'smallmoney' THEN 'double'
WHEN 'smallint' THEN 'int'
WHEN 'text' THEN 'string'
WHEN 'timestamp' THEN 'Date'
WHEN 'uniqueidentifier' THEN 'GUID'
--WHEN 'varbinary' THEN 'raw'
WHEN 'varchar' THEN 'string'
ELSE 'UNIDENTIFIED DataType ' + data_type + ''
END + ' ' + COLUMN_NAME + ' { get{return _'+COLUMN_NAME + '; } set{ _'+COLUMN_NAME + ' = value; } }'
FROM information_schema.columns
WHERE table_name = @tableName
UNION ALL
SELECT @LINEFEED+'#endregion'
END
GO
-- Author: Duminda Piumwardena
-- Create date: 10/10/2012
-- Description: To generate class properties from tables
-- =============================================
Alter PROCEDURE GenerateProperties
@tableName varchar(50)
AS
BEGIN
DECLARE @LINEFEED varchar(10)
SET @LINEFEED = CHAR(13)+CHAR(10)
SELECT '#region Fields'+@LINEFEED
UNION ALL
SELECT 'private ' +
CASE data_type
WHEN 'bigint' THEN 'int'
-- WHEN 'binary' THEN 'raw'
WHEN 'bit' THEN 'bool'
WHEN 'char' THEN 'char'
WHEN 'datetime' THEN 'DateTime'
WHEN 'decimal' THEN 'decimal'
WHEN 'float' THEN 'float'
-- WHEN 'image' THEN 'long raw'
WHEN 'int' THEN 'int'
WHEN 'money' THEN 'decimal'
WHEN 'nchar' THEN 'string'
WHEN 'ntext' THEN 'string'
WHEN 'nvarchar' THEN 'string'
WHEN 'numeric' THEN 'decimal'
WHEN 'real' THEN 'decimal'
WHEN 'smalldatetime' THEN 'DateTime'
WHEN 'smallmoney' THEN 'double'
WHEN 'smallint' THEN 'int'
WHEN 'text' THEN 'string'
WHEN 'timestamp' THEN 'Date'
WHEN 'uniqueidentifier' THEN 'GUID'
--WHEN 'varbinary' THEN 'raw'
WHEN 'varchar' THEN 'string'
ELSE 'UNIDENTIFIED DataType ' + data_type + ''
END + ' _' + COLUMN_NAME + ';'
FROM information_schema.columns
WHERE table_name = @tableName
UNION ALL
SELECT @LINEFEED+'#endregion'+@LINEFEED
UNION ALL
SELECT '#region Properties'+@LINEFEED
UNION ALL
SELECT 'public ' +
CASE data_type
WHEN 'bigint' THEN 'int'
-- WHEN 'binary' THEN 'raw'
WHEN 'bit' THEN 'bool'
WHEN 'char' THEN 'char'
WHEN 'datetime' THEN 'DateTime'
WHEN 'decimal' THEN 'decimal'
WHEN 'float' THEN 'float'
-- WHEN 'image' THEN 'long raw'
WHEN 'int' THEN 'int'
WHEN 'money' THEN 'decimal'
WHEN 'nchar' THEN 'string'
WHEN 'ntext' THEN 'string'
WHEN 'nvarchar' THEN 'string'
WHEN 'numeric' THEN 'decimal'
WHEN 'real' THEN 'decimal'
WHEN 'smalldatetime' THEN 'DateTime'
WHEN 'smallmoney' THEN 'double'
WHEN 'smallint' THEN 'int'
WHEN 'text' THEN 'string'
WHEN 'timestamp' THEN 'Date'
WHEN 'uniqueidentifier' THEN 'GUID'
--WHEN 'varbinary' THEN 'raw'
WHEN 'varchar' THEN 'string'
ELSE 'UNIDENTIFIED DataType ' + data_type + ''
END + ' ' + COLUMN_NAME + ' { get{return _'+COLUMN_NAME + '; } set{ _'+COLUMN_NAME + ' = value; } }'
FROM information_schema.columns
WHERE table_name = @tableName
UNION ALL
SELECT @LINEFEED+'#endregion'
END
GO
Comments