Monday, May 19, 2014

Simply creating the properties based on the SQL table structure

Step 1:- Copy this code and past in your sql editer

declare @TableName sysname = '@TableName'
declare @result varchar(max) = 'public class ' + @TableName + '
{'

select @result = @result + '
    public ' + ColumnType + ' ' + ColumnName + ' { get; set; }
'
from
(
    select
        replace(col.name, ' ', '_') ColumnName,
        column_id,
        case typ.name
            when 'bigint' then 'long'
            when 'binary' then 'byte[]'
            when 'bit' then 'bool'
            when 'char' then 'string'
            when 'date' then 'DateTime'
            when 'datetime' then 'DateTime'
            when 'datetime2' then 'DateTime'
            when 'datetimeoffset' then 'DateTimeOffset'
            when 'decimal' then 'decimal'
            when 'float' then 'float'
            when 'image' then 'byte[]'
            when 'int' then 'int'
            when 'money' then 'decimal'
            when 'nchar' then 'char'
            when 'ntext' then 'string'
            when 'numeric' then 'decimal'
            when 'nvarchar' then 'string'
            when 'real' then 'double'
            when 'smalldatetime' then 'DateTime'
            when 'smallint' then 'short'
            when 'smallmoney' then 'decimal'
            when 'text' then 'string'
            when 'time' then 'TimeSpan'
            when 'timestamp' then 'DateTime'
            when 'tinyint' then 'byte'
            when 'uniqueidentifier' then 'Guid'
            when 'varbinary' then 'byte[]'
            when 'varchar' then 'string'
            else 'UNKNOWN_' + typ.name
        end ColumnType
    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
    where object_id = object_id(@TableName)
) t
order by column_id

set @result = @result  + '
}'


print @result


Step 2:- Copy your table name and past at "@TableName"

Step 3:- OutPut



public class NewPawn
{
    public int Sno { get; set; }

    public int SnoPawn { get; set; }

    public DateTime Pawndate { get; set; }

    public string Name { get; set; }

    public string FatherHusbandName { get; set; }

    public string FullAddress { get; set; }

    public int AmountOfPrincipalOfLoan { get; set; }

    public string AmountOfPrincipalOfLoanInWords { get; set; }

    public string Particulars { get; set; }

    public int PayInterestForEvery3Months { get; set; }

    public int MyAnnualIncome1 { get; set; }

    public int MyAnnualIncome2 { get; set; }

    public DateTime DateOfExpiry { get; set; }

    public string Kg1 { get; set; }

    public string Kg2 { get; set; }

    public string G1 { get; set; }

    public string G2 { get; set; }

    public string MG1 { get; set; }

    public string MG2 { get; set; }

    public string PV1 { get; set; }

    public string PV2 { get; set; }

    public byte[] SignatureOwner { get; set; }

    public byte[] SignatureClient { get; set; }

    public bool Status { get; set; }

    public string CreatedBy { get; set; }

    public DateTime CreatedDate { get; set; }

    public string LastModifiedBy { get; set; }

    public DateTime LastModifiedDate { get; set; }

}



For any clarifications or queries please don’t hesitate to call or Email

Phone :- + 91-9492179390


Email :- lannam@technobrainltd.com (or) lokeshtec@gmail.com 

No comments:

Post a Comment