2

Is there any way to get some list of all Sql Data Types on c#?

I know that we have a Enum called SQlDbType which contains all Sql Data Types, but can we convert an Enum to an array of strings?

I only want to get some list with all Sql Data Types, instead of writing them one by one and adding them into my array of strings.

1

1 Answer 1

7

Enum to an array of strings is pretty easy:

var names = Array.ConvertAll((SqlDbType[]) Enum.GetValues(typeof(SqlDbType)),
                             type => type.ToString());

or using LINQ:

var names = Enum.GetValues(typeof(SqlDbType))
                .Cast<SqlDbType>()
                .Select(x => x.ToString())
                .ToArray();

Or more pleasantly using my Unconstrained Melody library, either as an array or (more efficiently) an immutable list:

string[] names = Enums.GetNamesArray<SqlDbType>();

IList<string> namesList = Enums.GetNames<SqlDbType>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.