c# - Bitwise OR to define SqlBulkCopyOptions? -


so asked 1 of senior dev figure out transaction scope related problem , came desk , used different overload on sqlbulkcopy , sqlbulkcopyoptions parameter did below:

sqlbulkcopyoptions options = (sqlbulkcopyoptions.firetriggers | sqlbulkcopyoptions.checkconstraints); 

it works now, don't bitwise or means here. thought had understanding of it, never used it, usage made me scratch head. yep, didn't ask senior explain me.. hoping me understand statement does. of bitwise or examples on net numbers, (i think), this?

an enum can used flag if has [flags] attribute , has enum values independent of each other:

the definition of sqlbulkcopyoptions here: https://github.com/microsoft/referencesource/blob/master/system.data/system/data/sqlclient/sqlbulkcopyoptions.cs

ignore use of bitshift syntax. actual values are:

name              hex  dec  pow 2   binary default          =  0 =  0 =    0 = 00000000 keepidentity     =  1 =  1 =    1 = 00000001 checkconstraints =  2 =  2 =    2 = 00000010 tablelock        =  4 =  4 =    3 = 00000100 keepnulls        =  8 =  8 =    4 = 00001000 firetriggers     = 10 = 16 =    5 = 00010000 useinternaltxn   = 20 = 32 =    6 = 00100000 

observe each value next power of 2, means in binary (last column) bits mutually exclusive.

this means can combine them in way lets see each value set, example, if want keepidentity , tablelock, that's 0x01 0x04. use or operator, on per-bit basis, gives behaviour want:

in binary:

00000001 00000100 or -------- 00000101 

observe how first , third bits 1.

thus, (keepidentity | tablelock == 5).

this approach not work enum values not powers of two, example, if keepidentity had value of 1 , checkconstraints had value of 2 tablelock had value of 3, in binary are:

00000001 keepidentity 00000010 checkconstraints 00000011 tablelock 

observe analysing bits of 00000011 impossible determine if combination of keepidentity , checkconstraints, or single tablelock value. why flags enum values must be: 1. powers of 2, , 2: mutually-exclusive (with exceptions shorthand , combination values).


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -