c# - 为什么 SqlParameter 名称/值构造函数将 0 视为 null?

标签 c# sql-server ado.net sqlparameter

我在一段代码中观察到一个奇怪的问题,即席 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/

相关文章:

c# - 使用 ASP.NET Core Entity Framework 在数据层中进行数据加密

sql - 使用 SQL 根据不同月份聚合年度数据

sql - 海量数据从SQL Server导出到VB中的Excel

sql-server - 使用commit之前退出存储过程是否有害?

c# - 从数据行中检索日期时间值 (C#)

c# - 如何判断对象是否为枚举?

C#:是否可以取消 linq2sql 查询?

c# - 如何从存储过程返回一个数组?

c# - 在多线程应用程序中正确使用 IOC(装饰器或拦截器?)

c# - 在 C# 中使用默认泛型值