In this blog, I will demonstrate how to convert a table column into a C# model class, using stored procedure. This is a very useful phenomenon for most of the C# programmers.

SQL Code

Create table and column as you need, like given below.

  1. CREATE TABLE [dbo].[EmployeeMaster](
  2.     [RowId] [bigint] NULL,
  3.     [EmpFirstName] [varchar](50) NULL,
  4.     [EmpLastName] [varchar](50) NULL,
  5.     [PhoneNo] [bigint] NULL,
  6.     [City] [bigint] NULL,
  7.     [Address] [varchar](500) NULL,
  8.     [DateOfBirth] [datetime] NULL,
  9.     [Gender] [int] NULL,
  10.     [MaritalStatus] [bit] NULL,
  11.     [EmpStatus] [bit] NULL
  12. ) ON [PRIMARY]
  13. GO

Usually, you can create class model with the same name as table columns. Often, you look at the table structure and then create property. But now, you just have to pass the table name and types of class.

SQL Code

  1. CREATE PROCEDURE CREATEMODEL
  2. (
  3.      @TableName SYSNAME ,
  4.      @CLASSNAME VARCHAR(500)
  5. )
  6. AS
  7. BEGIN
  8.     DECLARE @Result VARCHAR(MAX)
  9.     SET @Result = @CLASSNAME + @TableName + ‘
  10. {‘
  11. SELECT @Result = @Result + ‘
  12.     public ‘ + ColumnType + NullableSign + ‘ ‘ + ColumnName + ‘ { get; set; }’
  13. FROM
  14. (
  15.     SELECT
  16.         REPLACE(col.NAME, ‘ ‘‘_’) ColumnName,
  17.         column_id ColumnId,
  18.         CASE typ.NAME
  19.             WHEN ‘bigint’ THEN ‘long’
  20.             WHEN ‘binary’ THEN ‘byte[]’
  21.             WHEN ‘bit’ THEN ‘bool’
  22.             WHEN ‘char’ THEN ‘string’
  23.             WHEN ‘date’ THEN ‘DateTime’
  24.             WHEN ‘datetime’ THEN ‘DateTime’
  25.             WHEN ‘datetime2’ then ‘DateTime’
  26.             WHEN ‘datetimeoffset’ THEN ‘DateTimeOffset’
  27.             WHEN ‘decimal’ THEN ‘decimal’
  28.             WHEN ‘float’ THEN ‘float’
  29.             WHEN ‘image’ THEN ‘byte[]’
  30.             WHEN ‘int’ THEN ‘int’
  31.             WHEN ‘money’ THEN ‘decimal’
  32.             WHEN ‘nchar’ THEN ‘char’
  33.             WHEN ‘ntext’ THEN ‘string’
  34.             WHEN ‘numeric’ THEN ‘decimal’
  35.             WHEN ‘nvarchar’ THEN ‘string’
  36.             WHEN ‘real’ THEN ‘double’
  37.             WHEN ‘smalldatetime’ THEN ‘DateTime’
  38.             WHEN ‘smallint’ THEN ‘short’
  39.             WHEN ‘smallmoney’ THEN ‘decimal’
  40.             WHEN ‘text’ THEN ‘string’
  41.             WHEN ‘time’ THEN ‘TimeSpan’
  42.             WHEN ‘timestamp’ THEN ‘DateTime’
  43.             WHEN ‘tinyint’ THEN ‘byte’
  44.             WHEN ‘uniqueidentifier’ THEN ‘Guid’
  45.             WHEN ‘varbinary’ THEN ‘byte[]’
  46.             WHEN ‘varchar’ THEN ‘string’
  47.             ELSE ‘UNKNOWN_’ + typ.NAME
  48.         END ColumnType,
  49.         CASE
  50.             WHEN col.is_nullable = 1 and typ.NAME in (‘bigint’‘bit’‘date’‘datetime’‘datetime2’‘datetimeoffset’‘decimal’‘float’‘int’‘money’‘numeric’‘real’‘smalldatetime’‘smallint’‘smallmoney’‘time’‘tinyint’‘uniqueidentifier’)
  51.             THEN ‘?’
  52.             ELSE 
  53.         END NullableSign
  54.     FROM SYS.COLUMNS col join sys.types typ on col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
  55.     where object_id = object_id(@TableName)
  56. ) t
  57. ORDER BY ColumnId
  58. SET @Result = @Result  + ‘
  59. }’
  60. print @Result
  61. END

After running this procedure, execute SP with parameters. Now, the result will be loaded as model class.

  1. exec CREATEMODEL ‘EmployeeMaster’‘public class ‘

Output

  1. public class EmployeeMaster
  2. {
  3. public long? RowId { getset; }
  4. public string EmpFirstName { getset; }
  5. public string EmpLastName { getset; }
  6. public long? PhoneNo { getset; }
  7. public long? City { getset; }
  8. public string Address { getset; }
  9. public DateTime? DateOfBirth { getset; }
  10. public int? Gender { getset; }
  11. publicbool ? MaritalStatus { getset; }
  12. publicbool ? EmpStatus { getset; }
  13. }
Advertisements