I observed a strange problem in a piece of code where an adhoc SQL query was not producing the expected output, even though its parameters matched records in the data source. I decided to enter the following test expression into the immediate window:
new SqlParameter("Test", 0).Value
This gave a result of null
, which leaves me scratching my head. It seems that the SqlParameter
constructor treats zeroes as nulls. The following code produces the correct result:
SqlParameter testParam = new SqlParameter();
testParam.ParameterName = "Test";
testParam.Value = 0;
// subsequent inspection shows that the Value property is still 0
Can anyone explain this behaviour? Is it somehow intentional? If so, it's potentially rather dangerous...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…