0

I've come to an issue where i need to insert bit values(true/false) in my database for each hour for student attendance. For each hour there should be one value(true/false).

The straight forward option is create 24 columns and input value for each of them, every time a student is present. But definately this is also the worst one.

Someone suggest me to use binary field for it, one field could contain all these values.

Actually mu gui has 24 checkboxes and whichever checkbox is checked, its value should be stored as 1 so when next time i open the screem those checkboxes should remain checked.

I am using sql server 2008 and .net framework 4.0, if it helps.

Thanks

3

2 Answers 2

1

There is no need to mess with [var]binary(n). Since you only need 24 bits, store a regular, non-nullable int. Then just do bit-math on your code, i.e. shift (<< / >>) operators and bitwise combination (&, | and maybe some ~). Nothing else is needed. So if we decide that the LSB is 00:00, working right-to-left, then someone present at 03:00, and 14:00-16:00 (inclusive) would have a value of

(1 << 3) | (1 << 14) | (1 << 15) | (1 << 16) ==> 114696

If you aren't comfortable with bit-math, then either:

  • get comfortable with bit-math
  • don't try storing it as binary

You also state:

But definately this is also the worst one.

In what way? It describes your scenario, and SQL-server will condense multiple bit fields for you automatically. That might actually not be a bad option.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the BitArray class for manipulating the individual bits of an int (and other types). This may make it easier for you to go with the binary option, however I don't think its necessarily a bad option to store all the values in the database.. Why do you think this is such a bad option?

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.