most seamless way to store a C# enum value in a SQL Server database?

B

Bennett Haselton

One of the most convenient features of Visual Studio .Net and ASP.Net
is being able to add rows to a SQL Server database in an
intellisense-safe way, with code like:

newRow.Setparent_post_idNull();
newRow.post_text = this.lblMessageBody.Text;
newRow.post_datetime = System.DateTime.Now;

dsLocalDataSet1.user_post.Adduser_postRow(newRow);
this.oleDbUserPostAdapter.Update(dsLocalDataSet1.user_post);

If I want my "row" objects to contain an enumerated type and to still
be stored in the SQL Server database table, is there a seamless way to
do that without sacrificing all the benefits of typed datasets and the
code they automatically generate as in the examples above?

If I can't do it with true enums (called, for example,
PostStatus.AwaitingResponse and PostStatus.HasBeenRepliedTo), then I
could have a PostStatus class with public static constant strings
PostStatus.AwaitingResponse, whose value is "AwaitingResponse", and
PostStatus.HasBeenRepliedTo, whose value is "HasBeenRepliedTo", and
then have the type of that data field simply be a string when stored
to the database, and then when reading from the database, compare the
string value to PostStatus.AwaitingResponse or to
PostStatus.HasBeenRepliedTo to see what it is. (Storing them as ints
would make sorting and other operations faster, but storing them as
strings would make it easier to see what the actual values are when
viewing the database table directly.) Just wondering what the pros
usually do in this case!

-Bennett
 
B

bruce barker

the best is to create a domain table with the int value and enum name, then
store the actual int value in your tables.

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top