Here's the problem:
I'm working with Visual Studio and made a Winforms application. This is supposed to work with a database. Reading the database using a SQL Query works totally fine without any problems.
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)
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...
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.