Two Inner Joins in OleDb SQL query [duplicate] - c#

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

Related

System.Data.OleDb.OleDbException: Undefined function 'ConcatRelated' in expression

I'm working on a c# winform software, connecting to local MS Access db (.mdb)
Connection string used is Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb and it's kinda considered a requirement, Ace is not an option.
I'm trying to use the function ConcatRelated but keep encountering error:
System.Data.OleDb.OleDbException (0x80040E14): Undefined function 'ConcatRelated' in expression.
My query below:
string carColumnsAndTable = "SELECT c.Id, cust.CustomerName as InsuredName, c.CarPlate as VehicleNo, c.CarMake as Make, c.CarModel as Model, c.CarYear as VehicleYear, c.CarCc as CC, c.CarChasisNumber as Chasis, c.CarEngineNumber as EngineNumber, "
+ "ConcatRelated('d.DebtorName', 'CarDebtor cd left join Debtor d on cd.DebtorId = d.Id', 'cd.CarId = ' & [c.Id]) as Debtor "
+ "FROM ((Car as c) left join Customer as cust on c.CustomerId = cust.Id) ";
And function setup as in screenshot below:
Query works fine in ms access:
Anyone got any clue on what I should do to resolve this?

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

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.

C# Windows Forms inner join three tables

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

SQLException at Inner Join

I am using a C# program that calls a SQL statement that is executed on an instance of SQL Server 2008 R2. Here is the SQL Call:
SELECT TOP 1 as1.AssetTagID, as1.TagID, as1.CategoryID, as1.Description,
as1.HomeLocationID, as1.ParentAssetTagID
FROM Assets AS as1 ORDER BY ar.DateScanned DESC
INNER JOIN AssetsReads AS ar
ON as1.AssetTagID = ar.AssetTagID
WHERE (ar.ReadPointLocationID='Readpoint1' OR ar.ReadPointLocationID='Readpoint2')
AND as1.TagID!='000000000000000000000000';
I am getting an SQLException around INNER. The exception text is the following:
System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'INNER'.
I can put the stack trace on here as well, but I felt like it would clutter the question. Here is the actual string in the C# code that I am using with the call:
"SELECT TOP 2 as1.AssetTagID, as1.TagID, " +
"as1.CategoryID, as1.Description, as1.HomeLocationID," +
"as1.ParentAssetTagID FROM Assets AS as1 ORDER BY ar.DateScanned DESC\n" +
"INNER JOIN AssetsReads AS ar ON as1.AssetTagID = ar.AssetTagID\n" +
"WHERE (ar.ReadPointLocationID='" + IntegrationService.Lane2Zones[0] +
"' OR ar.ReadPointLocationID='" + IntegrationService.Lane2Zones[1] + "')\n" +
"AND as1.TagID!='000000000000000000000000';"
Your ORDER BY statement can't be there. Move it to the end
I'll also give the obligatory "Don't do this" speech. Concatenating SQL strings like this opens you up to SQL injection attacks. There is plenty of information about that on SO and Google so I won't go into it, but you should definitely consider making this a parameterized query.
Like he said... your order by clause was out of order :)
SELECT TOP 1 as1.AssetTagID,
as1.TagID,
as1.CategoryID,
as1.Description,
as1.HomeLocationID,
as1.ParentAssetTagID
FROM Assets AS as1
INNER JOIN AssetsReads AS ar
ON as1.AssetTagID = ar.AssetTagID
WHERE ( ar.ReadPointLocationID = 'Readpoint1'
OR ar.ReadPointLocationID = 'Readpoint2' )
AND as1.TagID != '000000000000000000000000'
ORDER BY ar.DateScanned DESC;
I'll also note that using schema qualified objects is recommended by Microsoft (http://technet.microsoft.com/en-us/library/ms190387(v=sql.105).aspx). Also you should use parenthesis around your top (value) statement.
SELECT TOP (1) [as1].[AssetTagID],
[as1].[TagID],
[as1].[CategoryID],
[as1].[Description],
[as1].[HomeLocationID],
[as1].[ParentAssetTagID]
FROM [<schema>].[Assets] AS [as1]
INNER JOIN [<schema>].[AssetsReads] AS [ar]
ON [as1].AssetTagID = [ar].[AssetTagID]
WHERE ( [ar].[ReadPointLocationID] = 'Readpoint1'
OR [ar].[ReadPointLocationID] = 'Readpoint2' )
AND cast([as1].TagID AS [INT]) != 0
ORDER BY [ar].[DateScanned] DESC;

SQL error in C#

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.."

Categories