mardi 6 octobre 2020

Get SQL type name into a string from a SqlDbType enum

Assume I have a SqlDbType variable named Foo. I want to create a CREATE SQL statement, but the first column in the SQL of will be from type Foo, which means, for example, that if Foo is SqlDbType.Int, then the SQL statement will be:

CREATE TABLE BAR(
 FirstColl INT
);

This is how I do it in my code

 StringBuilder SQL = new StringBuilder("");

            foreach (var entity in entities)
            {
                SQL.Append($"CREATE TABLE ");
                SQL.Append($"{entity.Name} ({Environment.NewLine}");
                foreach (var field in entity.fields)
                {
                    //field name
                    SQL.Append(field.Name + " ");
                    
                    //SQLTypeConvertor.ConvertToSQLType returns SqlDbType, which I want to convert to the respective string, here is where I am stuck
SQL.Append(SQLTypeConvertor.ConvertToSQLType(field.GetType().GetGenericArguments()[0]));
                }
            }

So my problem is, really, converting the enum SqlDbType to the type in SQL string of which he represents.

How can I do that?





Aucun commentaire:

Enregistrer un commentaire