I'm building a C# desktop app with a SQLite database attached. When i run a simple query in C# and store it in DataTable it gives different results than the SQLite Expert does. The expected results are the SQLite Expert ones.
Involving tables:
CREATE TABLE [pr_PrintCost] (
[ID] INTEGER PRIMARY KEY NOT NULL,
[ID_Type] INT NOT NULL CONSTRAINT [pr_PrintType] REFERENCES "pr_PaperType"([ID]),
[Step] INT NOT NULL,
[Cost] [DOUBLE PRECISION] NOT NULL);
CREATE TABLE [pr_PaperType] (
[ID] INTEGER NOT NULL PRIMARY KEY,
[PaperType] CHAR NOT NULL);
CREATE TABLE [pr_PaperSize] (
[ID] INTEGER PRIMARY KEY NOT NULL,
[PaperSize] CHAR NOT NULL);
CREATE TABLE [pr_PrintType] (
[ID] INTEGER NOT NULL PRIMARY KEY,
[ID_Size] INT NOT NULL CONSTRAINT [FK_PaperSize] REFERENCES [pr_PaperSize]([ID]),
[Color] BOOLEAN NOT NULL DEFAULT 0);
Table data:
INSERT INTO pr_PaperSize (ID, PaperSize)
VALUES (1, 'A4'),
(2, '25x35'),
(3, 'MAX-A3'),
(4, 'BANNER');
INSERT INTO pr_PaperType (ID, PaperType)
VALUES (1, 'ΒΕΛΒΕΤ'),
(2, 'ΜΑΤ ΜΠΡΙΣΤΟΛ'),
(3, 'ΑΥΤΟΚΟΛΛΗΤΟ ΜΑΤ'),
(4, 'ΑΥΤΟΚΟΛΛΗΤΟ ΓΥΑΛΙΣΤΕΡΟ'),
(5, 'ΦΩΣΦΟΡΟΥΧΟ ΑΥΤΟΚΟΛΛΗΤΟ'),
(6, 'ΔΙΑΦΑΝΕΣ ΑΥΤΟΚΟΛΛΗΤΟ'),
(7, 'MAJESTIC MARBLE WHITE'),
(8, 'ΚΡΑΦΤ'),
(9, 'ΑΚΟΥΑΡΕΛΛΑ'),
(10, 'ΡΙΖΟΧΑΡΤΟ');
INSERT INTO pr_PrintType (ID, ID_Size, Color)
VALUES (1, 1, False),
(2, 1, True),
(3, 2, False),
(4, 2, True),
(5, 3, False),
(6, 3, True),
(7, 4, False),
(8, 4, True);
INSERT INTO pr_PrintCost (ID, ID_Type, Step, Cost)
VALUES (1, 1, 1, 0.04),
(2, 1, 30, 0.04),
(3, 1, 200, 0.04),
(4, 2, 1, 0.04),
(5, 2, 50, 0.04),
(6, 2, 100, 0.04),
(7, 2, 200, 0.04),
(8, 3, 1, 0.04),
(9, 3, 30, 0.04),
(10, 3, 200, 0.04),
(11, 4, 1, 0.04),
(12, 4, 10, 0.04),
(13, 4, 200, 0.04),
(14, 5, 1, 0.04),
(15, 5, 30, 0.04),
(16, 5, 200, 0.04),
(17, 6, 1, 0.04),
(18, 6, 50, 0.04),
(19, 6, 100, 0.04),
(20, 6, 200, 0.04),
(21, 7, 1, 0.04),
(22, 7, 8, 0.04),
(23, 7, 50, 0.04),
(24, 8, 1, 0.04),
(25, 8, 13, 0.04),
(26, 8, 25, 0.04),
(27, 8, 50, 0.04);
The query:
SELECT ps.ID AS ID_PaperSize, ps.PaperSize AS PaperSize, pt.Color AS Color, pc.Step AS Step, pc.Cost AS Cost
FROM pr_PrintCost pc
INNER JOIN pr_PrintType pt ON pc.ID_Type = pt.ID
INNER JOIN pr_PaperSize ps ON pt.ID_Size = ps.ID
ORDER BY ps.ID, pt.Color
SQLite gives the following results (Step & Cost actual values are irrelevant) :
ID_PaperSize PaperSize Color Step Cost
1 A4 False SomeInteger SomeDecimal
1 A4 False SomeInteger SomeDecimal
1 A4 False SomeInteger SomeDecimal
1 A4 True SomeInteger SomeDecimal
1 A4 True SomeInteger SomeDecimal
1 A4 True SomeInteger SomeDecimal
1 A4 True SomeInteger SomeDecimal
2 25x35 False SomeInteger SomeDecimal
2 25x35 False SomeInteger SomeDecimal
2 25x35 False SomeInteger SomeDecimal
2 25x35 True SomeInteger SomeDecimal
2 25x35 True SomeInteger SomeDecimal
2 25x35 True SomeInteger SomeDecimal
3 MAX-A3 False SomeInteger SomeDecimal
3 MAX-A3 False SomeInteger SomeDecimal
3 MAX-A3 False SomeInteger SomeDecimal
3 MAX-A3 True SomeInteger SomeDecimal
3 MAX-A3 True SomeInteger SomeDecimal
3 MAX-A3 True SomeInteger SomeDecimal
3 MAX-A3 True SomeInteger SomeDecimal
4 BANNER False SomeInteger SomeDecimal
4 BANNER False SomeInteger SomeDecimal
4 BANNER False SomeInteger SomeDecimal
4 BANNER True SomeInteger SomeDecimal
4 BANNER True SomeInteger SomeDecimal
4 BANNER True SomeInteger SomeDecimal
4 BANNER True SomeInteger SomeDecimal
But C# calling the same query from a .sql file give these results:
ID_PaperSize PaperSize Color Step Cost
1 A4 True SomeInteger SomeDecimal
2 25x35 True SomeInteger SomeDecimal
3 MAX-A3 True SomeInteger SomeDecimal
4 BANNER True SomeInteger SomeDecimal
c# code:
public static DataTable GetDataTable(string sql)
{
DataTable dt = new DataTable();
try
{
using(SQLiteConnection cnn = new SQLiteConnection(dbConnection))
{
cnn.Open();
using(SQLiteCommand mycommand = new SQLiteCommand(cnn))
{
mycommand.CommandText = sql;
using(SQLiteDataReader reader = mycommand.ExecuteReader())
{
dt.Columns.Add("ID_PaperSize", typeof(Int32));
dt.Columns.Add("PaperSize", typeof(string));
dt.Columns.Add("Color", typeof(bool));
dt.Columns.Add("Step", typeof(Int32));
dt.Columns.Add("Cost", typeof(decimal));
dt.PrimaryKey = new DataColumn[] {
dt.Columns["ID_PaperSize"],
dt.Columns["Color"],
dt.Columns["Step"]
};
dt.Load(reader);
reader.Close();
}
}
cnn.Close();
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return dt;
}
Any Thoughts?
EDIT: The linked thread worked (edited C# code above) and thank you for that @CL