SQL query not working

I am trying to create a query in an access database for a C # dataset using the query editor, but the created method indicates a problem and was not created correctly.

SELECT Discs.*
FROM Discs
WHERE (Title=@Title OR @Title IS NULL) AND
(Type=@Type OR @Type IS NULL) AND
(ContainerID=@ContainerID OR @ContainerID IS NULL) AND
NOT (@Title IS NULL AND @Type IS NULL AND @ContainerID IS NULL)

error:

Generated SELECT statement.
Error in WHERE clause near '@'.
Unable to parse query text.

the generated selection method has no parameters and is not applicable. I tried the same SQL expression in the access request and it worked flawlessly, what should I do differently when passing it to C #?

+3
source share
3 answers

( , ,.NET 1.1 , , ), Jet OLEDB, ( ):

SELECT [Discs].*
FROM [Discs]
WHERE ([Title]=? OR ? IS NULL) AND
([Type]=? OR ? IS NULL) AND
([ContainerID]=? OR ? IS NULL) AND
NOT (? IS NULL AND ? IS NULL AND ? IS NULL)

( , , , , )

, , , , Parameters OleDbCommand.

+6

, , , :

  SELECT   *
  FROM     Discs
  WHERE 
           (Title = @Title OR @Title IS NULL) 
           AND (Type = @Type OR @Type IS NULL) 
           AND (ContainerID = @ContainerID OR @ContainerID IS NULL)
           AND (@Title IS NOT NULL OR @Type IS NOT NULL OR @ContainerID IS NOT NULL)

, " -null" IF, .

0

, , :

 Title = @Title OR @Title IS NULL 

 ISNULL(@Title,Title) = Title

It seems clearer to me, but it could be because I always saw it that way.

It also solves the problem with Mehrdad Afshari’s answer about the need to list the parameters several times. eg:

ISNULL(?,Title) = Title
0
source

Source: https://habr.com/ru/post/1749832/


All Articles