我在一段代码中观察到一个奇怪的问题,即席 SQL 查询没有产生预期的输出,即使它的参数与数据源中的记录匹配。我决定在即时窗口中输入以下测试表达式:
new SqlParameter("Test", 0).Value
这给出了 null
的结果,这让我摸不着头脑。 SqlParameter
构造函数似乎将零视为空值。以下代码产生正确的结果:
SqlParameter testParam = new SqlParameter();
testParam.ParameterName = "Test";
testParam.Value = 0;
// subsequent inspection shows that the Value property is still 0
谁能解释这种行为?这是故意的吗?如果是这样,它可能相当危险......
最佳答案
如 documentation 中所述对于那个构造函数:
When you specify an Object in the value parameter, the SqlDbType is inferred from the Microsoft .NET Framework type of the Object.
Use caution when you use this overload of the SqlParameter constructor to specify integer parameter values. Because this overload takes a value of type Object, you must convert the integral value to an Object type when the value is zero, as the following C# example demonstrates.
Parameter = new SqlParameter("@pname", (object)0);
If you do not perform this conversion, the compiler assumes that you are trying to call the SqlParameter (string, SqlDbType) constructor overload.
您只是调用了与您想象的不同的构造函数。
原因是 C# 允许从整数文字 0
进行隐式转换。枚举类型(下面只是整数类型),这种隐式转换导致 (string, SqlDbType)
构造函数比转换 int
所需的装箱转换更适合重载解析至 object
对于 (string, object)
构造函数。
当你传递 int
时,这永远不会成为问题变量,即使那个变量的值是0
(因为它不是零文字),或任何其他类型为 int
的表达式.如果您显式转换 int
也不会发生至 object
如上所示,因为只有一个匹配的重载。
关于c# - 为什么 SqlParameter 名称/值构造函数将 0 视为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8352260/