DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Migrating from Sakila-MySQL to Couchbase - Part 3: Stored Procedures
  • Migrating From ClickHouse to Apache Doris: What Happened?
  • Resolving Log Corruption Detected During Database Backup in SQL Server
  • NULL in Oracle

Trending

  • The API-Centric Revolution: Decoding Data Integration in the Age of Microservices and Cloud Computing
  • Reflections From a DBA
  • How TIBCO Is Evolving Its Platform To Embrace Developers and Simplify Cloud Integration
  • The Winds of Change: How Generative AI is Revolutionizing Cybersecurity
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Convert a Table Column Into a C# Model Class

How to Convert a Table Column Into a C# Model Class

If you're a C# programmer, you need to know how to convert a table column into a C# model class using stored procedures. And if you don't, you've come to the right place.

Thiruppathi Rengasamy user avatar by
Thiruppathi Rengasamy
·
Jul. 28, 17 · Tutorial
Like (3)
Save
Tweet
Share
18.41K Views

Join the DZone community and get the full member experience.

Join For Free

In this blog, I will demonstrate how to convert a table column into a C# model class using stored procedures. This is a very useful tip for any C# programmer.

SQL Code

Create tables and columns as you need, like given below:

CREATE TABLE [dbo].[EmployeeMaster](  
    [RowId] [bigint] NULL,  
    [EmpFirstName] [varchar](50) NULL,  
    [EmpLastName] [varchar](50) NULL,  
    [PhoneNo] [bigint] NULL,  
    [City] [bigint] NULL,  
    [Address] [varchar](500) NULL,  
    [DateOfBirth] [datetime] NULL,  
    [Gender] [int] NULL,  
    [MaritalStatus] [bit] NULL,  
    [EmpStatus] [bit] NULL  
) ON [PRIMARY]  
GO  

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

Now check out this SQL code:

CREATE PROCEDURE CREATEMODEL  
(  
     @TableName SYSNAME ,  
     @CLASSNAME VARCHAR(500)   
)  
AS  
BEGIN  
    DECLARE @Result VARCHAR(MAX)  

    SET @Result = @CLASSNAME + @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  

END  

After running this procedure, execute the stored procedure with parameters. The result will be loaded as a model class:

exec CREATEMODEL 'EmployeeMaster', 'public class '  

Output

public class EmployeeMaster  
{  
	public long? RowId { get; set; }  
	public string EmpFirstName { get; set; }  
	public string EmpLastName { get; set; }  
	public long? PhoneNo { get; set; }  
	public long? City { get; set; }  
	public string Address { get; set; }  
	public DateTime? DateOfBirth { get; set; }  
	public int? Gender { get; set; }  
	public bool? MaritalStatus { get; set; }  
	public bool? EmpStatus { get; set; }  
}  

And that's it!

Database sql Convert (command)

Opinions expressed by DZone contributors are their own.

Related

  • Migrating from Sakila-MySQL to Couchbase - Part 3: Stored Procedures
  • Migrating From ClickHouse to Apache Doris: What Happened?
  • Resolving Log Corruption Detected During Database Backup in SQL Server
  • NULL in Oracle

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: