<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Database Table in SQL Server - MSSQL Query</title>
	<atom:link href="https://mssqlquery.com/tag/database-table-in-sql-server/feed" rel="self" type="application/rss+xml" />
	<link>https://mssqlquery.com</link>
	<description>MSSQL and TSQL Programming and TSQL Examples</description>
	<lastBuildDate>Thu, 14 Apr 2022 16:16:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.2</generator>

<image>
	<url>https://mssqlquery.com/wp-content/uploads/2023/06/cropped-mssql-query-icon-32x32.png</url>
	<title>Database Table in SQL Server - MSSQL Query</title>
	<link>https://mssqlquery.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Creating Entity Class from Database Table in SQL Server</title>
		<link>https://mssqlquery.com/creating-entity-class-from-database-table-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Thu, 14 Apr 2022 16:16:40 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Creating Entity Class from Database Table]]></category>
		<category><![CDATA[Database Table in SQL Server]]></category>
		<category><![CDATA[Entity Class from Database Table in SQL Server]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=881</guid>

					<description><![CDATA[<p>Hello to everyone, In this article, I will try to give information about creating Entity Class from database table in SQL Server. In SQL Server, in some cases you may want to create Entity Class from database table. You can easily do this using the code below. DECLARE @TableName sysname&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/creating-entity-class-from-database-table-in-sql-server">Creating Entity Class from Database Table in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello to everyone,</p>
<p>In this article, I will try to give information about creating Entity Class from database table in SQL Server.</p>
<p>In SQL Server, in some cases you may want to create Entity Class from database table.</p>
<p>You can easily do this using the code below.</p>
<pre class="line-numbers"><code class="language-sql">DECLARE @TableName sysname = 'Categories';

DECLARE @Result VARCHAR(MAX) = 'public class ' + @TableName + ' 

{';

SELECT @Result = @Result + ' 

    public '     + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; } 

'
FROM
(
    SELECT REPLACE(col.name, ' ', '_') ColumnName,
           column_id ColumnId,
           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,
           CASE
               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'
                                    ) THEN
                   '?'
               ELSE
                   ''
           END NullableSign
    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 ColumnId;

SET @Result = @Result + ' 

}';

PRINT @Result;</code></pre>
<p>When you run the above code, you will see a result similar to the one below.</p>
<p><img decoding="async" fetchpriority="high" class="alignnone wp-image-883 size-full" src="https://mssqlquery.com/wp-content/uploads/2022/04/creating-entity-class-from-database-table-in-sql-server-1.jpg" alt="" width="700" height="567" srcset="https://mssqlquery.com/wp-content/uploads/2022/04/creating-entity-class-from-database-table-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2022/04/creating-entity-class-from-database-table-in-sql-server-1-300x243.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>As you can see, Entity Class has been created from the database table.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 168</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/creating-entity-class-from-database-table-in-sql-server">Creating Entity Class from Database Table in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
