简体   繁体   English

尽管在例如 Access 中工作,SQL 查询却没有提供任何结果?

[英]SQL Query delivers no results despite working in e.g. Access?

Here's the problem:这是问题所在:

I'm working with Visual Studio and made a Winforms application.我正在使用 Visual Studio 并制作了一个 Winforms 应用程序。 This is supposed to work with a database.这应该与数据库一起使用。 Reading the database using a SQL Query works totally fine without any problems.使用 SQL 查询读取数据库工作正常,没有任何问题。

cmd.CommandText = 
     "SELECT Buecher.Bu_ISBN AS ISBN, 
             Buecher.Bu_Titel AS Titel,  
             Buecher.Bu_Originaltitel AS Originaltitel, 
             Buecher.Bu_Buchreihe AS Buchreihe, 
             Buecher.Bu_Genre AS Genre, 
             Autor.Au_Vorname AS [Autor-Vorname], 
             Autor.Au_Nachname AS[Autor-Nachname], 
             Verlag.Ve_Name AS Verlag 
      FROM ((((Buecher 
               INNER JOIN [Buch-Autor] 
                 ON Buecher.Bu_ID = [Buch-Autor].Bu_ID) 
               INNER JOIN Autor 
                 ON [Buch-Autor].Au_ID = Autor.Au_ID) 
               INNER JOIN [Buch-Verlag] 
                 ON Buecher.Bu_ID = [Buch-Verlag].Bu_ID) 
               INNER JOIN Verlag 
                 ON [Buch-Verlag].Ve_Name = Verlag.Ve_Name)";
        ausgabe();

(it is in German, ausgabe() is a method that transfers the Query into a DataGridView - works fine) (它是德语,ausgabe() 是一种将 Query 传输到 DataGridView 的方法 - 工作正常)

Later in the program the user shall search with various criteria using text boxes, that's where it doesn't work anymore, the Query simply is not executed.稍后在程序中,用户将使用文本框使用各种条件进行搜索,这就是它不再起作用的地方,只是不执行查询。

if (optBuch.Checked == true)
        {
            cmd.CommandText = 
               "SELECT Buecher.Bu_ISBN AS ISBN, 
                       Buecher.Bu_Titel AS Titel, 
                       Buecher.Bu_Originaltitel AS Originaltitel, 
                       Buecher.Bu_Buchreihe AS Buchreihe, 
                       Buecher.Bu_Genre AS Genre, 
                       Autor.Au_Vorname AS [Autor-Vorname], 
                       Autor.Au_Nachname AS [Autor-Nachname], 
                       Verlag.Ve_Name AS Verlag 
                FROM ((((Buecher 
                         INNER JOIN [Buch-Autor] 
                            ON Buecher.Bu_ID = [Buch-Autor].Bu_ID) 
                         INNER JOIN Autor 
                            ON [Buch-Autor].Au_ID = Autor.Au_ID) 
                         INNER JOIN [Buch-Verlag] 
                            ON Buecher.Bu_ID = [Buch-Verlag].Bu_ID) 
                         INNER JOIN Verlag 
                            ON [Buch-Verlag].Ve_Name = Verlag.Ve_Name)  
              WHERE (Buecher.Bu_ISBN = '%" + txtOpt1.Text + "%') 
                AND (Buecher.Bu_Titel = '%" + txtOpt2.Text + "%') 
                AND (Buecher.Bu_Originaltitel = '%" + txtOpt3.Text + "%') 
                AND (Buecher.Bu_Buchreihe = '%" + txtOpt4.Text + "%') 
                AND (Buecher.Bu_Genre = '%" + txtOpt5.Text + "%') 
                AND (Autor.Au_Vorname = '%" + txtOpt6.Text + "%') 
                AND (Autor.Au_Nachname = '%" + txtOpt7.Text + "%') 
                AND (Verlag.Ve_Name = '%" + txtOpt8.Text + "%')";

I tried various forms of the Query but until now no one worked.我尝试了各种形式的查询,但直到现在都没有人工作。 It works totally fine when I delete the whole " Where " section...当我删除整个“ Where ”部分时,它完全正常......

I hope I could express myself in a comprehensible way.我希望我能用一种可以理解的方式表达自己。

You cannot use the operand " = " if you want to use a wildcard.如果要使用通配符,则不能使用操作数“=”。 Replace " = " with "like".将“=”替换为“喜欢”。

So it would looks something like this:所以它看起来像这样:

WHERE(Buecher.Bu_ISBN like '%" + txtOpt1.Text + "%') AND
(Buecher.Bu_Titel like '%" + txtOpt2.Text + "%')

Beside the use of LIKE as suggested by @The Integrator you probably need OR instead of AND除了@The Integrator 建议的LIKE使用之外,您可能需要OR而不是AND

Otherwise the row has to match all the criteria to be returned.否则该行必须匹配所有要返回的条件。

          WHERE (Buecher.Bu_ISBN = '%" + txtOpt1.Text + "%') 
            OR (Buecher.Bu_Titel = '%" + txtOpt2.Text + "%') 
            OR (Buecher.Bu_Originaltitel = '%" + txtOpt3.Text + "%') 
            OR (Buecher.Bu_Buchreihe = '%" + txtOpt4.Text + "%') 
            OR (Buecher.Bu_Genre = '%" + txtOpt5.Text + "%') 
            OR (Autor.Au_Vorname = '%" + txtOpt6.Text + "%') 
            OR (Autor.Au_Nachname = '%" + txtOpt7.Text + "%') 
            OR (Verlag.Ve_Name = '%" + txtOpt8.Text + "%')";

Also you need use parameter, otherwise you are vulnerable to Sql Injection attacks还需要使用参数,否则容易受到Sql Injection 攻击

https://stackoverflow.com/a/5165985/3470178 https://stackoverflow.com/a/5165985/3470178

暂无
暂无

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

相关问题 在数据访问代码(例如 Dapper)中最小化/管理 SQL 字符串 blob 的最佳实践 - Best practices to Minimize/Manage SQL string blobs in data access code (e.g. Dapper) 使用单个巨型任务杀死/中止线程(例如SQL查询) - Killing/Aborting a thread with a single, giant task (e.g. SQL Query) SQL:将例如1,2,4,5,7修正为1,2,3,4,5(队列顺序) - SQL: Correct e.g. 1,2,4,5,7 to 1,2,3,4,5 (queue order) IntelliSense不适用于omnisharp,但不能用于其他扩展(例如F#) - IntelliSense not working for omnisharp but for rest of extensions (e.g. F#) C#如何将模糊输入的对象(例如“ var”)作为特定类型的对象(例如“ Form”)访问 - C# How to access an ambiguously typed object (e.g. 'var') as a specifically typed object (e.g. 'Form') 当对存在的目录使用 ListDirectoryDetails 时,FtpWebRequest 返回“550 文件不可用(例如文件未找到,无法访问)” - FtpWebRequest returning "550 File unavailable (e.g. file not found, no access)" when using ListDirectoryDetails for a directory that exists 远程服务器返回错误:(550) .NET 中的文件不可用(例如,找不到文件,无法访问) - The remote server returned an error: (550) File unavailable (e.g., file not found, no access) in .NET 远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限) - The remote server returned an error: (550) File unavailable (e.g., file not found, no access) 构造大字符串(例如,对于SQL命令)C#编译器有多聪明? - Constructing big strings (e.g. for SQL commands) how smart is the C# compiler? 如何在不打开套接字的情况下访问Windows Phone 7特定的网络类型(例如EDGE,3G等)? - How to access Windows Phone 7 specific network type (e.g. EDGE, 3G, etc.) without opening a socket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM