Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

Searching in SQL Server with C#. There are many textbox and many probability

[Search Form]1

I will do a search in the database, if there are sections that are empty, should I type a query one by one? If all the pieces are filled in, my code will be:

SELECT * 
FROM Thesis AS T
INNER JOIN Person AS A ON A.PersonID = T.AuthorID 
                       AND A.PersonFullName = 'Author name'
INNER JOIN Person AS S ON S.PersonID = T.SupervisorID 
                       AND S.PersonFullName = 'supervisor'
LEFT OUTER JOIN Person AS CS ON CS.PersonID = T.SupervisorID 
                             AND CS.PersonFullName = 'cosupervisor'
INNER JOIN Enstitute AS E ON E.EnstituteID = T.EnstituteID 
                          AND E.EnstituteName = ' enstitutename '
INNER JOIN University AS U ON U.UniversityID = E.UniversityID 
                           AND U.UniversityName='universityname '
WHERE 
    Title = 'title' AND 
    ThesisNo = 123 AND 
    [Type] = 'type' AND
    [Language] = ' Language' AND
    [SubmitDate] BETWEEN 2000 AND 2021  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use something like this (I don't know what DB are you using, this is just an algoritm):

-- Input parameters:

 @Author VARCHAR(50);   
 @Supervisor VARCHAR(50);  
 @Enstitute VARCHAR(50);   
 @University VARCHAR(50);

------
SELECT * 
FROM Thesis AS T
INNER JOIN Person AS A ON A.PersonID = T.AuthorID 
                    
INNER JOIN Person AS S ON S.PersonID = T.SupervisorID 
                     
LEFT OUTER JOIN Person AS CS ON CS.PersonID = T.SupervisorID 
                           
INNER JOIN Enstitute AS E ON E.EnstituteID = T.EnstituteID 
                        
INNER JOIN University AS U ON U.UniversityID = E.UniversityID 
    
WHERE 
    (@Author is null or A.PersonFullName = @Author
   AND 
 ( @SuperVisor is null or  S.PersonFullName = @Supervisor)
  AND 
    (@Enstitute is null or   E.EnstituteName = @Enstitute
 AND 
    (@University is null or  U.UniversityName=@University)
  And

  Title = 'title' AND 
    ThesisNo = 123 AND 
    [Type] = 'type' AND
    [Language] = ' Language' AND
    [SubmitDate] BETWEEN 2000 AND 2021  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...