Syntax error (missing operator) in query expression c# (access database) - c#

Hello! I tried to connect 4 tables form Access in Visual Basic c# and I got "Syntax error (missing operator) in query expression", can you please help me? Thanks!
string query = "Select e.Denumire_Ech, e.Descriere_Ech, e.UnitateMasura, e.Pret_Vanzare, o.Cantitate_EchOf From ECHIPAMENTE e INNER JOIN OFERTE o ON e.Cod_Echipament = o.Cod_Echipament INNER JOIN CONTRACTE c ON c.Cod_Oferta = o.Cod_Oferta INNER JOIN FACTURI f ON f.Nr_Contract = c.Nr_Contract WHERE Nr_Contract='" + CB_Contract.Text + "'";

You have multiple tables you are joining across but have not specified which table your WHERE clause is looking up on.
string query = "Select e.Denumire_Ech, e.Descriere_Ech, e.UnitateMasura, e.Pret_Vanzare, o.Cantitate_EchOf From ECHIPAMENTE e INNER JOIN OFERTE o ON e.Cod_Echipament = o.Cod_Echipament INNER JOIN CONTRACTE c ON c.Cod_Oferta = o.Cod_Oferta INNER JOIN FACTURI f ON f.Nr_Contract = c.Nr_Contract WHERE c.Nr_Contract='" + CB_Contract.Text + "'";
Also you should watch out for SQL injection and unusual characters in the CB_Contract.Text For example if there is a ' character then the sql will fail.

Related

SQL Query to MySQL Query

Well, I was watching a video tutorial on youtube about making a program with c# and using a database. The guy was using SQL commands while I was using MySQL through phpmyadmin (XAMPP is love).
I've managed to keep going until I reached one part where the guy was creating stored procedures and input this code:
create procedure datosfactura #NumFac int
as
select
F.*, D.PrecioVen, D.CanVe, C.Nom_cli, A.Nom_pro, D.PrecioVen * D.CanVe as importe
from
Facturas F inner join Detalles D on F.NunFac = D.NumFac
inner join Articula A on D.CodPro = A.id_pro
inner join Cliente C on F.CodCli = C.id_clientes
where F.NumFac = #NumFac
I racket my brain and tried different ways but I can figure out what I'm doing wrong.
As it is, if I make a new MySQL command inputting that query it returns that there's an error in the first line.
What is the right way to convert this SQL query into MySQL? Thank you for your help.
Try this:
delimiter //
create procedure datosfactura(NumFac int)
begin
select F.*,
D.PrecioVen,
D.CanVe,
C.Nom_cli,
A.Nom_pro,
D.PrecioVen * D.CanVe as importe
from Facturas F
inner join Detalles D on F.NunFac = D.NumFac
inner join Articula A on D.CodPro = A.id_pro
inner join Cliente C on F.CodCli = C.id_clientes
where F.NumFac = NumFac;
end//
delimiter ;
Useful articles:
http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx
https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html
Maybe something like this (i dont have access to mysql now)
delimiter $$
create function datosfactura(IN NumFac int)
BEGIN
select
F.*, D.PrecioVen, D.CanVe, C.Nom_cli, A.Nom_pro, D.PrecioVen * D.CanVe as
importe
from
Facturas F inner join Detalles D on F.NunFac = D.NumFac
inner join Articula A on D.CodPro = A.id_pro
inner join Cliente C on F.CodCli = C.id_clientes
where F.NumFac = NumFac
end $$

MySQL Inner Join Syntax Error in C#

Anyone know why got syntax error? I think there are no reserve words inside right?
C# Command
cmd.CommandText = "SELECT ordertable._name, ordertable.quantity, food_menu.food_price, beverage_menu.beverage_price" +
"FROM ordertable" +
"INNER JOIN food_menu ON ordertable._name = food.foodname" +
"INNER JOIN beverage_menu ON beverage_menu.beverage_name' = ordertable._name" +
"WHERE ordertable.tablenum = '1'";
Error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'JOIN food_menu ON ordertable._name = food.foodnameINNER JOIN
beverage_menu ON be' at line 1
You are missing spaces. At the moment your query will be like FROM ordertableINNER JOIN food_menu .... You could use the multiline syntax to make it simpler
cmd.CommandText = #"SELECT ordertable._name, ordertable.quantity, food_menu.food_price, beverage_menu.beverage_price
FROM ordertable
INNER JOIN food_menu ON ordertable._name = food_menu.foodname
INNER JOIN beverage_menu ON beverage_menu.beverage_name = ordertable._name
WHERE ordertable.tablenum = 1";
Remove unnecessary concatenation from string, you are getting an error because your query needs some spaces which you have not specified before join.
cmd.CommandText = "SELECT ordertable._name,
ordertable.quantity,
food_menu.food_price,
beverage_menu.beverage_price
FROM ordertable
INNER JOIN food_menu
ON ordertable._name = food.foodname
INNER JOIN beverage_menu
ON beverage_menu.beverage_name = ordertable._name
WHERE ordertable.tablenum = 1";

Performing Subquery using Query Builder in Dataset C#

I'm using this sql statement to produce the desired result. And would like to display those result query in my report in C# using rdlc via Dataset(.xsd).
SELECT pay.Cutoff,
emp.Id,
emp.LastName,
emp.FirstName,
emp.MiddleName,
emp.TinNumber,
job.Rate * 25 AS FixBIR,
(SELECT COUNT(*) AS MonthsWorked
FROM payroll AS pay3
WHERE YEAR(pay3.DateGenerated) = 2014
AND pay3.EmployeeId = 1
AND pay3.Cutoff = 1
ORDER BY MONTH(pay3.DateGenerated) ASC) * (job.Rate * 25)
AS MonthsWorked_FixBIR_TODATE,
pay.TaxWithheld,
(SELECT SUM(payroll2.TaxWithheld) AS TaxTotal
FROM employee AS employee2
INNER JOIN payroll AS payroll2
ON employee2.Id = payroll2.EmployeeId
WHERE (payroll2.Cutoff = 1)
AND (employee2.Id = emp.Id)
AND YEAR(payroll2.DateGenerated) = 2014)
AS Tax_TODATE,
YEAR(pay.DateGenerated) AS YEAR
FROM employee AS emp
INNER JOIN payroll AS pay
ON emp.Id = pay.EmployeeId
INNER JOIN job
ON emp.JobId = job.Id
WHERE pay.Cutoff = 1
AND pay.PayrollMonth = 'August'
AND Year(pay.DateGenerated) = 2014
This statement works fine (tested in navicat).
However, when I transferred this to Dataset using Query Builder it doesn't work. The error says:
The wizard detects the following problems when configuring the TableAdapter: "Fill" Details:
!Generated SELECT statement
Error in SELECT clause: expression near 'SELECT'
Error in SELECT clause: expression near 'FROM'
Missing FROM Clause
Error in SELECT clause: expression near ','.
Unable to parse query text
And if I try to use a simple subquery in the SELECT statement, an error says:
The query cannot be represented graphically in the Diagram and Criteria Pane.
What do I do for me to use the sql statement in rdlc? Are there other ways aside from query builder in dataset?

Using Subquery in System.Data.SqlCommand (C#) - Syntax Error, working in SQL Server Mgmt Studio

I got a query used by a Webservice which contains a subquery. When executing the query in SQL Server Management Studio, it works fine. However, using this in the webservice as a SqlCommand, I get:
System.Data.SqlClient.SqlException: Incorrect Syntax near the keyword 'JOIN'.
Incorrect Syntax near the keyword 'AS'.
Inserting extra characters let me figure out that the error refers to the position right after the subquery.
The query is as following:
SELECT H.H_ID AS ID, H.Name, TD.TDValue, A.A_ID, A_C.Value
FROM
H CROSS JOIN
A JOIN
(SELECT TD.A_ID, TD.H_ID, MAX(cast(TD.Version AS bigint)) AS Version FROM TD GROUP BY TD.A_ID, TD.H_ID) AS TData ON TData.H_ID = H.H_ID AND TData.A_ID = A.A_ID LEFT JOIN
TD2 ON TD2.H_ID = H.H_ID AND TD2.A_ID = A.A_ID AND TD2.Version = TData.Version LEFT JOIN
A_C ON A_C.A_ID = A.A_ID AND cast(A_C.R AS CHAR) = cast(TD.TDValue AS CHAR)
WHERE (H.Info = 1);
The Subquery is needed to get the last Version of an entry of the table TD.
If there are better ways to do that, feel free to let me know as well. :-)
Are there any limitations with the SqlCommand in C# that do not exist in MS SQL, or am I doing something wrong?
I would appreciate any help so much!
I would think that using a common table expression would be more efficient in this case. I'm not 100% that this is exactly what you need as I don't know your database structure and data needs, but this should help.
;with
cte_Tdata AS
(
SELECT
TD.A_ID
,TD.H_ID
,MAX(cast(TD.Version AS bigint)) AS Version
FROM
TD
GROUP BY
TD.A_ID
,TD.H_ID
),cte_TD AS
(
SELECT
TD.A_ID
,TD.H_ID
,TD.TDValue
FROM
TD
)
SELECT H.H_ID AS ID, H.Name, cte_TD.TDValue, A.A_ID, A_C.Value
FROM
H CROSS JOIN
A JOIN cte_Tdata ON cte_Tdata.H_ID = H.H_ID
AND cte_Tdata.A_ID = A.A_ID
INNER JOIN cte_TD ON cte_Tdata.H_ID = cte_TD.H_ID
AND cte_Tdata.A_ID = cte_TD.A_ID
LEFT JOIN A_C ON A_C.A_ID = A.A_ID
AND cast(A_C.R AS CHAR) = cast(cte_TD.TDValue AS CHAR)
LEFT JOIN TD2 ON TD2.H_ID = H.H_ID
AND TD2.A_ID = A.A_ID
AND TD2.Version = cte_Tdata.Version
WHERE
H.Info = 1;

sql code error when put in the c# code

A friend of mine has a problem, she has done a project in c# and everything looks fine, but the problem is that, she gets error with the SQL query when she puts it in the code, but when we try the code in microsoft SQL Server it works.
Here is thee example f the code where the error shows:
DataSet dsDelP = new DataSet();
string sql = "";
sql = "SELECT Grupi_ID, (p.emri + ' ' + p.mbiemri) as Profesori , k.EmriKlases as Klasa, d.emri as Dita, Ora_fillimit as Fillimi, Ora_mbarimit as Mbarimi";
sql += "FROM grupetOraret gro";
sql += "inner join Profesori p on gro.Profesori_ID=p.Profesor_ID";
sql += "inner join Klasa k on gro.Klasa_ID=k.Klasa_ID";
sql += "inner join Dita d on gro.ID_Dita=d.ID_dita";
SqlDataAdapter daDelP = new SqlDataAdapter(sql, con);
daDelP.Fill(dsDelP, "grupetOraret");
dataGridViewKerkoOrarin.DataSource = dsDelP.Tables[0].DefaultView;
You have no spaces. You should add them in every line (in its beggining or ending). This is how it should look like, assuming the query itself is correct (as soon as you checked it already):
sql = "SELECT Grupi_ID, (p.emri + ' ' + p.mbiemri) as Profesori , k.EmriKlases as Klasa, d.emri as Dita, Ora_fillimit as Fillimi, Ora_mbarimit as Mbarimi ";
sql += "FROM grupetOraret gro ";
sql += "inner join Profesori p on gro.Profesori_ID=p.Profesor_ID ";
sql += "inner join Klasa k on gro.Klasa_ID=k.Klasa_ID ";
sql += "inner join Dita d on gro.ID_Dita=d.ID_dita ";
Appending to the sql variable doesn't automatically insert newlines, so sql will end up looking like
...FROM grupetOraret groinner join Profesori p on gro.Profesori_ID=p.Profesor_ID...
Note that grupetOraret gro and inner join have no space between them.
Try using # for multi-line string literals, makes things easier and more efficient:
string sql = #"
SELECT Grupi_ID, (p.emri + ' ' + p.mbiemri) as Profesori , k.EmriKlases as Klasa, d.emri as Dita, Ora_fillimit as Fillimi, Ora_mbarimit as Mbarimi
FROM grupetOraret gro
inner join Profesori p on gro.Profesori_ID=p.Profesor_ID
inner join Klasa k on gro.Klasa_ID=k.Klasa_ID
inner join Dita d on gro.ID_Dita=d.ID_dita";
Your SQL looks like this after the strings are concatenated:
SELECT ... as MbarimiFROM .... on gro.Profesori_ID=p.Profesor_IDinner ...
You need a space between the last word on each line and the first word on the next line:
string sql = "... as Mbarimi";
sql += " FROM ...";
sql += " inner join Profesori p on gro.Profesori_ID=p.Profesor_ID";
sql += " inner join Klasa k on gro.Klasa_ID=k.Klasa_ID";
sql += " inner join Dita d on gro.ID_Dita=d.ID_dita";

Categories