Blankman
Blankman

Reputation: 267220

Retrieving a byte array stored in a INT field in the database

how do I retrieve the byte array stored in an integer column in my database?

Do I first case it as int, then as byte[] ?

byte[] permissions = (byte) Convert.ToInt(dr["myField"]);

?

Upvotes: 0

Views: 647

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502806

Well, the data is stored as an integer. How do you want to convert that into a byte array. Given an input of, say, 12345, what should the output be?

Why are you trying to store a byte array in an integer column in the first place?

EDIT: Now that we know that it's basically flags you're after, I wouldn't convert it to a byte array, I'd use an enum:

[Flags]
public enum Permissions
{
    Read = 1,
    Write = 2,
    Execute = 4,
    Guillotine = 8,
    Lynch = 16
    // etc
}

Then you can just cast:

Permissions permissions = (Permissions) (int) dr["Field"]

Upvotes: 1

Related Questions