I have an SQL statement in C # (.NET Framework 4 working with SQL Server 2k8) that looks like this:
SELECT [Column1] FROM [Table1] WHERE [Column2] = @Column2
The above query works fine with the following ADO.NET code:
DbParameter parm = Factory.CreateDbParameter();
parm.Value = "SomeValue";
parm.ParameterName = "@Column2";
This query returns null rows, though if I assign DBNull.Value to the DbParameter Value member, even if column 2 has null values. If I changed the query to specifically set the null test:
SELECT [Column1] FROM [Table1] WHERE [Column2] IS @Column2
I get "Invalid syntax near exception @@ Column2" at runtime. Can't I use null or DBNull as a parameter in a WHERE clause of a SELECT statement?
source
share