Monday, 6 March 2023

Generate C#/.NET Class atomically from SQL Script

Let's learn how to generate C# Classes from SQL Scripts. Generate C# classes automatically.


Step 1: Copy the query below and run it in the SQL Editor (New Query).
Step 2: Pass the table name as a parameter and run the query.

You can find the entire class in the output below.

DECLARE @TableName sysname = 'Person' -- Pass Table Name here

DECLARE @Output VARCHAR(MAX) = 'public class ' + @TableName + ' {' SELECT @Output = @Output + ' public ' + ColumnType + NullSign + ' ' + 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 'double' WHEN 'image' THEN 'byte[]' WHEN 'int' THEN 'int' WHEN 'money' THEN 'decimal' WHEN 'nchar' THEN 'string' WHEN 'ntext' THEN 'string' WHEN 'numeric' THEN 'decimal' WHEN 'nvarchar' THEN 'string' WHEN 'real' THEN 'float' WHEN 'smalldatetime' THEN 'DateTime' WHEN 'smallint' THEN 'short' WHEN 'smallmoney' THEN 'decimal' WHEN 'text' THEN 'string' WHEN 'time' THEN 'TimeSpan' WHEN 'timestamp' THEN 'long' 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 NullSign FROM sys.columns col INNER 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) ) temp order by ColumnId set @Output = @Output + ' }' print @Output
Here is the output













If you want to generate the entire database classes in a single go then follow the below article

Generate C# classes of the entire database


I hope you like this article.


1 comment:

  1. Microsoft Azure Administrator Associate AZ-104 Certification Exam:

    Know the exam objectives: Review the official exam objectives provided by Microsoft to understand the topics and skills that will be tested on the exam.

    Study the Azure documentation: Use the official Azure documentation to learn about the different Azure services and features. Practice using the Azure portal and CLI to become familiar with the different tools.

    Take practice exams: Use online resources or practice exams to get a feel for the types of questions you can expect on the actual exam.

    Join a study group or forum: Join a study group or online forum to ask questions, share tips, and get support from other exam takers.

    Stay up to date: Keep up with the latest updates and changes to Azure by reading the official Azure blog and other relevant news sources.

    Get hands-on experience: Practice setting up and managing Azure resources in a test environment to gain practical experience with the platform.

    Take breaks and manage your time: Take regular breaks while studying and manage your time effectively to avoid burnout and maximize your productivity.

    Remember to remain focused, persistent, and disciplined in your studies, and you should be able to pass the AZ-104 Braindumps exam in 2023.

    ReplyDelete

Implement Authorization in Swagger with Static Value in Header .Net 8

If you want an anonymous user should not run the APIs. To run your API Endpoints From Swagger / Postman / Code the user should pass the head...