Ana Ameer
Ana Ameer

Reputation: 681

SQL Server - Using tinyint with enum

Suppose that I have a SQL Server database that, amongst other tables, has a table named Mail, which has the expected recipient, sender, message body, etc. associated with it, one of these being the property Priority, which basically indicates if a mail has a high, normal, or low priority of being sent. Since I'll be only using these small amount of values (and if more were to be added it wouldn't be that many), I decided to make Priority a tinyint type, while in my code I'd represent is an enum type.

My question is, what is the right way to insert and retrieve this property when calling the database? In the case of retrieval (using a DataRow) I have:

mail.Priority = (MailPriority)(byte)row["Priority"];

And for inserting:

parameter = new SqlParameter("@Priority", SqlDbType.TinyInt);
parameter.Value = (byte)mail.Priority;
statement.Parameters.Add(parameter);

(Where statement is a SQLCommand).

So would this work, and is it the right way to do it? I figured that using tinyint would be okay, since the possible values can't be that many, but I don't know if int would be fine, better in terms of performance, or more commonly used, etc.

Upvotes: 3

Views: 4290

Answers (1)

paparazzo
paparazzo

Reputation: 45106

Performance and space are not the same.
From a performance perspective .NET can actually access an Int32 faster.
What you get is space savings in SQL.
And you should also define you enum as byte.

public enum enumSVMV : byte { SV = 0, MV = 1, none = 2 };

Upvotes: 10

Related Questions