简体   繁体   中英

Inner Join of more than one table in C# with SQLite

I need to do an Inner Join with more than one table in my C# program.

I've tried to execute my query using DBeaver in the same database, and it works. But when I use that same query in my C# code, it returns nothing. I know the connection works, since I get data from one of the tables, and that does fill. Also, it did work just doing one inner join. I use the SQLite NuGet Package.

This is my query (the one I used in DBeaver):

SELECT mcc.ModeloId, t.FctrlClusterId, mcc.FctrlClusterDescription, cl.ClusterId, cl.ClusterName, 
t.Command, co.CommandName, t.AttributeId, at.AttributeName, t.DataPosition, t.DataType, t.TramaData
FROM Trama t
INNER JOIN ModeloClusterCanal mcc ON t.FctrlClusterId = mcc.FctrlClusterId 
INNER JOIN Cluster cl ON mcc.ClusterId = cl.ClusterId
INNER JOIN Command co ON t.Command = co.CommandId
INNER JOIN Attribute at ON t.AttributeId = at.AttributeId
WHERE RxTx LIKE 'Tx'

And this is my C# code (with the same query, but using String.Format() ):


private static readonly string tablaCluster = "Cluster";
private static readonly string tablaCommand = "Command";
private static readonly string tablaAttribute = "Attribute";
private static readonly string tablaModeloClusterCanal = "ModeloClusterCanal";
private static readonly string tablaTrama = "Trama";

CommandText = String.Format("SELECT mcc.ModeloId, t.FctrlClusterId, mcc.FctrlClusterDescription, cl.ClusterId, cl.ClusterName, t.Command, " +
    "co.CommandName, t.AttributeId, at.AttributeName, t.DataPosition, t.DataType, t.TramaData " +
    "FROM {0} t " +
    "INNER JOIN {1} mcc ON t.FctrlClusterId = mcc.FctrlClusterId " +
    "INNER JOIN {2} cl ON mcc.ClusterId = cl.ClusterId " +
    "INNER JOIN {3} co ON t.Command = co.CommandId " +
    "INNER JOIN \"{4}\" at ON t.AttributeId = at.AttributeId " +
    "WHERE t.RxTx LIKE 'Tx'", 
    tablaTrama,
    tablaModeloClusterCanal,
    tablaCluster,
    tablaCommand,
    tablaAttribute);

    SqliteAdapter = new SQLiteDataAdapter(CommandText, SqliteConnection);
    SqliteAdapter.Fill(DataSet, "TramasTx");

(before this code, I open the connection and fill the DataSet with another table)

I also tried using the query string directly, without formatting it, but it gave the same result.

In DBeaver, I get the desired columns and the rows I want.

In C#, the Table with which I fill the DataSet has 0 rows.

I tried using parenthesis as said in INNER JOIN to more than one table , but it didn't work either. I really can't see what can cause this.

In case the ER diagram of the DB could help, I can edit the question and add it.


I edited the CommandText since I left some parenthesis that shouldn't be there, and I also tried resetting the SqliteCommand variable (because I used it before, with another command), but this didn't work either.我编辑了 CommandText,因为我留下了一些不应该在那里的括号,我还尝试重置 SqliteCommand 变量(因为我之前用过另一个命令),但这也不起作用。 I'm gonna try using another variable for the SQLiteCommand, since I don't know if reusing it could affect the result?


I wasn't using at all the SQLiteCommand variable so got rid of it. 我根本没有使用 SQLiteCommand 变量,所以摆脱了它。 I tried disposing of the SQLiteAdapter (since I do use it for the table data I get before), but it didn't work either. It doesn't give me any error or exception, it just creates the Table inside the DataSet, but the row count is 0.


I couldn't find the problem with the query. 我找不到查询的问题。 I talked with a colleage and they told me that, since the query works on programs like DBeaver, it might be because the driver in .NET works different. In the end I just collected each table in different DataTables and got the info I needed iterating and using if control statements. I hope this helps someone.

there are 2 issues

  1. Why you used inverted quotes in table name ( Attribute ) remove the quotes.
  2. In where clause add alias or table name before column name WHERE table.RxTx LIKE 'Tx'"

Hope this will help full for you

It's an old question, and I think I didn't find an appropriate answer, but I'm copy-pasting my last EDIT since it seems that it's the way I resolved my issue.

I couldn't find the problem with the query. I talked with a colleage and they told me that, since the query works on programs like DBeaver, it might be because the driver in .NET works different. In the end I just collected each table in different DataTables and got the info I needed iterating and using if control statements.

I'm marking this as my answer but won't close the post, in case someone finds the real issue at some point.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM