繁体   English   中英

具有DBNull.Value值的C#MySqlCommand参数不起作用

[英]C# MySqlCommand parameter with value of DBNull.Value not working

为什么在以下代码中DBNull.Value的值不起作用? 我不断得到错误的返回值,同时我确定有一条匹配的记录。

(machineNumber确实具有“ b”的值,所以这不是问题)

MySqlCommand cmd = new MySqlCommand("SELECT * FROM `sparter` WHERE `id` = @machineNumber AND `account_id` = @null", conn);

            cmd.Parameters.AddWithValue("@machineNumber", machineNumber);
            cmd.Parameters.AddWithValue("@null", DBNull.Value);

            using (var reader = cmd.ExecuteReader())
            {
                return reader.read();
            }

虽然此查询在MySqlWorkbench中有效:

SELECT * FROM `sparter` WHERE `id` = "b" AND `account_id` IS NULL

实际上,在MySQL中使用= or != null 使用IS NULLIS NOT NULL

您可以尝试一下:

MySqlCommand cmd = new MySqlCommand("SELECT * 
                                     FROM `sparter`
                                     WHERE `id` = @machineNumber AND `account_id` is @null", conn);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM