Logo

Welcome:

Welcome to Studer Consulting Group. At Studer Consulting Group we help companies solve problems through software. Whether you want a fully custom built application, implementation of Microsoft Dynamics 365 or an integration built between your current systems. We can handle it. With years of experience in the software development and Microsoft Dynamics 365, we can help your business grow.

Services We Offer:

(We also provide sub-contracting services in these areas.)

Microsoft Dynamics 365


  • Functional Consulting
  • Implementation
  • Plugin Development
  • Customization
  • Extending with Micrsoft Azure
  • CRM Portals
  • SSRS Reporting
  • Client-side Scripting (JavaScript)

System Integration \ Migration


  • API Development
  • Custom Coded
  • Scribe Online
  • KingswaySoft
  • SSIS

Software Development


  • .NET Core
  • ASP.NET Core
  • .NET
  • C#
  • JavaScript
  • Java
  • HTML
  • CSS
  • C\C++
  • SQL
  • XAML
  • Xamarin

Latest Blog Post:

SQL Script To Generate C# Model

6/5/2019 5:38:00 AM

While working on a data migration project that uses C# and Azure Functions.  I found myself in the need to generate a model of the table(s).  While doing a Google search I came across the following blog post that has a script to generate the model properties for me.  Simply replace the table name variable at the top of the script with the table name you want to generate the properties for.  You may also need to include the schema name in the table name if you have multiple tables with the same name, just under different schema's.  I did make one change to it in the select @ result line.  It was adding a blank line in between each property, so I removed the new line.


 declare @TableName sysname = 'TABLE_NAME'  
 declare @Result varchar(max) = 'public class ' + @TableName + '  
 {'  
 select @Result = @Result + '  
   public ' + (CASE WHEN ColumnName = 'RowVersion' THEN 'byte[]' ELSE ColumnType END) + 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 'string'  
       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 'timestamp'  
       when 'rowversion' then 'byte[]'  
       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  

Reference:
Habilis. (2017, May 01). Creating C# model class from SQL query. Retrieved from https://habilisbest.com/creating-c-model-class-from-sql-query

Contact Us:

(All fields are required.)