{"id":119,"date":"2017-01-20T14:30:22","date_gmt":"2017-01-20T11:30:22","guid":{"rendered":"http:\/\/uysalyilmaz.com\/wordpress\/?p=119"},"modified":"2017-01-20T14:30:32","modified_gmt":"2017-01-20T11:30:32","slug":"veritabanindaki-tablodan-class-uretmek-generate-class-from-database-table","status":"publish","type":"post","link":"http:\/\/uysalyilmaz.com\/index.php\/2017\/01\/20\/veritabanindaki-tablodan-class-uretmek-generate-class-from-database-table\/","title":{"rendered":"Veritaban\u0131ndaki Tablodan Class \u00dcretmek (Generate Class from database table)"},"content":{"rendered":"<pre class=\"lang:tsql decode:true \">declare @TableName sysname = 'TableName'\r\ndeclare @Result varchar(max) = 'public class ' + @TableName + '\r\n{'\r\n\r\nselect @Result = @Result + '\r\n    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }\r\n'\r\nfrom\r\n(\r\n    select \r\n        replace(col.name, ' ', '_') ColumnName,\r\n        column_id ColumnId,\r\n        case typ.name \r\n            when 'bigint' then 'long'\r\n            when 'binary' then 'byte[]'\r\n            when 'bit' then 'bool'\r\n            when 'char' then 'string'\r\n            when 'date' then 'DateTime'\r\n            when 'datetime' then 'DateTime'\r\n            when 'datetime2' then 'DateTime'\r\n            when 'datetimeoffset' then 'DateTimeOffset'\r\n            when 'decimal' then 'decimal'\r\n            when 'float' then 'float'\r\n            when 'image' then 'byte[]'\r\n            when 'int' then 'int'\r\n            when 'money' then 'decimal'\r\n            when 'nchar' then 'string'\r\n            when 'ntext' then 'string'\r\n            when 'numeric' then 'decimal'\r\n            when 'nvarchar' then 'string'\r\n            when 'real' then 'double'\r\n            when 'smalldatetime' then 'DateTime'\r\n            when 'smallint' then 'short'\r\n            when 'smallmoney' then 'decimal'\r\n            when 'text' then 'string'\r\n            when 'time' then 'TimeSpan'\r\n            when 'timestamp' then 'DateTime'\r\n            when 'tinyint' then 'byte'\r\n            when 'uniqueidentifier' then 'Guid'\r\n            when 'varbinary' then 'byte[]'\r\n            when 'varchar' then 'string'\r\n            else 'UNKNOWN_' + typ.name\r\n        end ColumnType,\r\n        case \r\n            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') \r\n            then '?' \r\n            else '' \r\n        end NullableSign\r\n    from sys.columns col\r\n        join sys.types typ on\r\n            col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id\r\n    where object_id = object_id(@TableName)\r\n) t\r\norder by ColumnId\r\n\r\nset @Result = @Result  + '\r\n}'\r\n\r\nprint @Result<\/pre>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/5873170\/generate-class-from-database-table\" target=\"_blank\">Kaynak<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>declare @TableName sysname = &#8216;TableName&#8217; declare @Result varchar(max) = &#8216;public class &#8216; + @TableName + &#8216; {&#8216; select @Result = @Result + &#8216; public &#8216; + ColumnType + NullableSign + &#8216; &#8216; + ColumnName + &#8216; { get; set; } &#8216; from ( select replace(col.name, &#8216; &#8216;, &#8216;_&#8217;) ColumnName, column_id ColumnId, case typ.name when &#8216;bigint&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[37,38],"class_list":["post-119","post","type-post","status-publish","format-standard","hentry","category-t-sql","tag-class","tag-tablo"],"_links":{"self":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":2,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":121,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts\/119\/revisions\/121"}],"wp:attachment":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}