conn.Open();
String sql = "select CATEGORIES.CAT_NAME,PRODUCTS.PRO_MODEL,PRODUCTS.PRO_NAME,PRODUCTS.PRO_PRICE,PRODUCTS.PRO_IMAGE,PRODUCTS.PRO_DESCRIPTION,PRODUCTS.PRO_STATUS,PRODUCTS.PRO_ACTIVE" +
"from PRODUCTS INNER JOIN CATEGORIES on PRODUCTS.CAT_ID = CATEGORIES.CAT_ID";
My Query SQL runs fine in MYSQL Server but it has error in C# code Incorrect syntax near the keyword 'INNER'. hope have answer soon
You need to add space between (.PRO_ACTIVE" and "from) string concatenation.
.PRO_ACTIVE " + "from...
So your query should be:
String sql = "select CATEGORIES.CAT_NAME,PRODUCTS.PRO_MODEL,PRODUCTS.PRO_NAME,PRODUCTS.PRO_PRICE,PRODUCTS.PRO_IMAGE,PRODUCTS.PRO_DESCRIPTION,PRODUCTS.PRO_STATUS,PRODUCTS.PRO_ACTIVE"
+ " " +//explicit space
"from PRODUCTS INNER JOIN CATEGORIES on PRODUCTS.CAT_ID = CATEGORIES.CAT_ID";
You need a space at the end of the first line of your string -- C# doesn't put line feeds in.
change it to:
String sql = "select CATEGORIES.CAT_NAME,PRODUCTS.PRO_MODEL,PRODUCTS.PRO_NAME,PRODUCTS.PRO_PRICE,PRODUCTS.PRO_IMAGE,PRODUCTS.PRO_DESCRIPTION,PRODUCTS.PRO_STATUS,PRODUCTS.PRO_ACTIVE" +
" from PRODUCTS INNER JOIN CATEGORIES on PRODUCTS.CAT_ID = CATEGORIES.CAT_ID";
A space was missing between "..PRODUCTS.PRO_ACTIVE" and "from PRODUCTS.."
Related
This question already has answers here:
SQL multiple join statement
(3 answers)
Closed 4 years ago.
I'm trying to make an SQL query with an OleDbCommand into an Access database (.accdb).
While this command works fine (in a OleDbCommand.ExecuteReader()):
string command =
"SELECT cred.* " +
"FROM TB_CREDENTIALS cred " +
"INNER JOIN TB_REL_USERS_CREDENTIALS rel ON cred.CRED_ID = rel.REL_USR_CRED_CRED_ID ";
This other doesn't, and I can't understand why (all examples I see around use the exact same syntax):
string command =
"SELECT cred.* " +
"FROM TB_CREDENTIALS cred " +
"INNER JOIN TB_REL_USERS_CREDENTIALS rel ON cred.CRED_ID = rel.REL_USR_CRED_CRED_ID " +
"INNER JOIN TB_USERS usr ON usr.USR_ID = rel.REL_USR_CRED_USER_ID ";
The exception given is the following System.Data.OleDb.OleDbException:
Syntax error (missing operator) in query expression 'cred.CRED_ID = rel.REL_USR_CRED_CRED_ID INNER JOIN TB_USERS usr ON usr.USR_ID = rel.REL_USR_CRED_USER_I' (message is cut here)
Version and details:
The database is a .accdb file created on Access 2010
The connection is created in C# with System.Data.OleDb.OleDbConnection
The connection provider is "Microsoft.ACE.OLEDB.12.0"
(This seems like a useless query, but of course I'll add a WHERE usr.SOME_FIELD = some_condition)
"For multi-table joins, you have to nest the extra joins in brackets:"
SQL multiple join statement
I have a problem with SQL query in C#.
In my database I have tables: Supplier, ApplicationForm, SupplierAdress. I was trying to join these three tables but I got an syntax error with the inner join.
Also my method in C# have input parameter AppFormID. With that parameter I have to open particular Application Form from database on Windows app.
This is my SQL query:
command.CommandText = "select ApplicationForm.Date,ApplicationForm.About,ApplicationForm.Supplier," +
" Supplier.IDSupplier,Supplier.Name,Supplier.Email,Supplier.Phone," +
" Supplier.SupplierAdress,SupplierAdress.AdressID,SupplierAdress.Name" +
" from ApplicationForm" +
" inner join Supplier on ApplicationForm.Supplier=Supplier.IDSupplier" +
" inner join SupplierAdress on ApplicationForm.Supplier.SupplierAdress = SupplierAdress .AdressID" +
" where ApplicationForm.ApplicatonFormID=" + AppFormID;
I hope that someone will help me.
This is wrong ApplicationForm.Supplier.SupplierAdress
try this, dunno exact field names:
from
ApplicationForm
inner join Supplier on ApplicationForm.Supplier=Supplier.IDSupplier
inner join SupplierAdress on Supplier.IdAdress =SupplierAdress.AdressID
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";
I am c# beginner and hopefully get a help here.
I am not using SQL Server but just a query put in c# as shown below:
SqlCommand custcmd = new SqlCommand("SELECT customer.customerid, warehouse.warehouseNo, warehouse.qty_goods, warehouse.qty_slack, warehouse.std_weight, (qty_goods+qty_slack)*std_weight/1000 AS Total
FROM Customer
INNER JOIN warehouse WHERE customer.customerid BETWEEN" + "textbox1.text" + "AND" + "textbox2.text", myconnection)
An error is shown "Incorrect syntax near WHERE"
Can anyone please comment and help?
Thank in advance
BETWEEN" + "textbox1.text" + "AND" + "textbox2.text"
if you put 1 and 3 in textbox1 and textbox2 it will produce
BETWEEN1AND3
so here is a error
DO:
BETWEEN " + "textbox1.text" + " AND " + "textbox2.text"
Or better:
string.Format("SELECT customer.customerid, warehouse.warehouseNo, warehouse.qty_goods, warehouse.qty_slack, warehouse.std_weight, (qty_goods+qty_slack)*std_weight/1000 AS Total
FROM Customer INNER JOIN warehouse WHERE customer.customerid BETWEEN {0} AND {1}", textbox1.Text,textbox2.Text)
using String.Format will prevent you from making such mistakes, because you see better what will be produced as query
After INNER JOIN warehouse you should add condition ON to link tables for example
INNER JOIN warehouse ON customer.<field> = warehouse.<field>
It is the reason of the error "Incorrect syntax near WHERE" but also you should change WHERE statement according to the #wudzik's answer
So I'm retrieving data from the database using odbc. One of my fields, which was causing the problem, is AutoNumber. In my query when I use, '" convert.toint64(empid)"', I get an error of data type missmatch. But when I used parameter, it worked perfectly. Can someone tell me what is the difference. As far as I know, AutoNumber is a long integer and long int is 64, right?
here are both of my queries:
string cmdText = "SELECT p.projID, p.projName, a.wageperday " +
"FROM ((projects p INNER JOIN assigns a ON p.projID = a.projname) " +
"INNER JOIN empos e ON a.employeeID= e.ID) " +
"WHERE a.employeeID = ?";
OdbcCommand assignslist = new OdbcCommand(cmdText, _connection);
assignslist.Parameters.AddWithValue("#empID", empid);
OdbcDataReader readassigns = assignslist.ExecuteReader();
GridView1.DataSource = readassigns;
GridView1.DataBind();
(this one works fine)
string cmdText = "SELECT p.projID, p.projName, a.wageperday " +
"FROM ((projects p INNER JOIN assigns a ON p.projID = a.projname) " +
"INNER JOIN empos e ON a.employeeID= e.ID) " +
"WHERE a.employeeID = '" + convert.toint64( empid ) + "';
OdbcCommand assignslist = new OdbcCommand(cmdText, _connection);
OdbcDataReader readassigns = assignslist.ExecuteReader();
GridView1.DataSource = readassigns;
GridView1.DataBind();
for this one, i get an error even if I removed the conversion, I get an error.
One more question, I don't understand INNER JOIN perfectly and the above code was from a user here who helped me. I don't understand why did he use "FROM ((projects p INNER JOIN assigns a ON p.projID = a.projname) " +
"INNER JOIN empos e ON a.employeeID= e.ID)"
two brackets -- I mean a bracket inside a bracket. And if I wanna join a 4th or 5th table do I have to put it inside a bracket aside from the main bracket? An example would be much appreciated!
If a.employeeID is a number then your problem are the quotes and the concatenation of a number to a string.
"WHERE a.employeeID = " + convert.toint64(empid).ToString()
but this make no sense because you have a string to build so, you could simply write
"WHERE a.employeeID = " + empid.ToString();
however use always the parameterized query. That is the correct way to go.
A parameterized query allows the Framework code to pass the parameters with the correct format and you don't have to worry about quoting values, format of dates and decimal separators. (And last but not least, you avoid any possibilities of Sql Injection)
For the second part of your question, JOIN is used to put togheter data from two tables, when you have more than one JOIN the parenthesys help to understand how the grouping from the tables are perfomed. First the data from projects and assigns are grouped together following the rules of the INNER JOIN then the resulting set of data is joined with the data from the employee table following the rules of the second join.
In your second query, instead of using convert.toint64( empid ) try using empid.ToString():
"WHERE a.employeeID = " + empid.ToString();
The error you were receiving was due to the fact that you were trying to concatenate a string with an integer.
With regards to the INNER JOINs you are using, the use of the brackets is dependant upon the database you are using. In most cases, you will not need the brackets at all and they can be removed without any issue, so you could rewrite the query to:
string cmdText = "SELECT p.projID, p.projName, a.wageperday " +
"FROM projects p " +
"INNER JOIN assigns a ON p.projID = a.projname " +
"INNER JOIN empos e ON a.employeeID = e.ID " +
"WHERE a.employeeID = " empid.ToString();
You're trying to add a string and an integer, which isn't allowed automatically. You'd have to convert the number to a string first, like this:
"WHERE a.employeeID = '" + empid.ToString() + "';
But, using parameters is the better way for other reasons (best habit to be in, to avoid SQL-injection attacks, etc.).