Archive for February, 2017

In C# Getting the Constant Value from Class using Reflection need not be a difficult and there are quite few articles and blogs written on the subject. I wanted to mimic the GetNameQuery function that is in Hibernate, maintaining backward compatibility while converting services to C# using Dapper. For Inspiration I looked at this blog. This returned a list of the constant Names and what I actually wanted to do was look through all the constant names and then match mine and return the Sql value inside.

Where to start: reflection

Firstly we need to look at the FieldInfo Class looking at the example it shows us how we can get the field values.

Read Full Article

Creating Dapper entities using MSSQL and Mapping classes is rather easy, but very time consuming from scratch. This came around because I really got tired of the typing a multitude of class and mapping files So let’s jump into it.

Firstly, lets go get the list of tables, this will return a list in the correct format for the T-SQL in clause. The reason why I did it this way was because it’s easier to remove those that I don’t want to generate entities for.

DECLARE @db varchar(50) = 'Your_db_name'
DECLARE @tempTable Table (TableName varchar(250))

Select distinct '''' +  ltrim(rtrim(t.table_name))+ ''',' as table_name
FROM     information_schema.columns c
         INNER JOIN information_schema.tables t
           ON c.table_name = t.table_name
              AND c.table_schema = t.table_schema
              AND t.table_type = 'BASE TABLE'

Read Full Article

This is just a short post in using Dapper ORM to Map column names in Mssql generating c# csharp code.

I had a requirement using Dapper ORM to Easily Map Column Names, problem was that the columns names had Underscores in them and the need to Have uppercase letter at the beginning of each word to conform to naming conventions. The service we are re-writing has numerous mapping tables so it all started out fine doing the tables with only a few columns, but as things cot more intense at 32 plus columns then I needed a better way to do it. MSSQL, to the rescue, I found a method on the web to convert the initial capitalisation for the beginning of each word for the I used this method:

CREATE FUNCTION [dbo].[InitCap] ( @InputString varchar(4000) ) 

DECLARE @Index          INT
DECLARE @Char           CHAR(1)
DECLARE @PrevChar       CHAR(1)
DECLARE @OutputString   VARCHAR(255)

SET @OutputString = LOWER(@InputString)
SET @Index = 1

WHILE @Index < = LEN(@InputString)
    SET @Char     = SUBSTRING(@InputString, @Index, 1)
    SET @PrevChar = CASE WHEN @Index = 1 THEN ' '
                         ELSE SUBSTRING(@InputString, @Index - 1, 1)

    IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
        IF @PrevChar != '''' OR UPPER(@Char) != 'S'
            SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))

    SET @Index = @Index + 1

RETURN @OutputString


Read Full Article