1

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

AkiS
  • 47
  • 2
  • 9
  • Show the table definitions, and some example records. Are you sure you're using the same database file in both cases? – CL. Jul 30 '14 at 06:57
  • Yes I am sure it's the same database and the same query in both cases. Actually c# results are as if i had a "GROUP BY ps.[ID]" in the statement *Added the information in the post – AkiS Jul 30 '14 at 20:01
  • The `pr_PrintType` table is missing; and to reproduce this, I'd need some table records. – CL. Jul 31 '14 at 06:44
  • Added missing table and insert statements – AkiS Jul 31 '14 at 09:09
  • What SQLite version are you using in C#? (see `SELECT sqlite_version();`) – CL. Jul 31 '14 at 09:22
  • I'm sorry for inactivity, I've been on the road the last days. The version is 3.8.5 – AkiS Aug 04 '14 at 14:04
  • Works for me in 3.8.5. What do you mean with "from a .sql file"? – CL. Aug 04 '14 at 14:10
  • Nothing weird. I mean a text file with .sql extention – AkiS Aug 04 '14 at 14:34
  • Show the code that executes that query. – CL. Aug 04 '14 at 14:41
  • Added the method that executes the query – AkiS Aug 04 '14 at 15:07
  • And the contents of the `sql` variable are really exactly this query? – CL. Aug 04 '14 at 15:07
  • These are debugging values: mycommand.CommandText = 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 – AkiS Aug 04 '14 at 15:43
  • I'm running out of ideas. How do you read the `DataTable`? – CL. Aug 04 '14 at 15:46
  • Actually when i see contents of reader while debugging, i can see all 27 rows. But when reader is loaded (in this line: "dt.Load(reader);"), dt has only 4. I am not publishing the results yet. At first I loaded them onto a Datatable and had //DEBUG MessageBoxes to check if they are correct. When problems began i just check datatables contents while debugging – AkiS Aug 04 '14 at 15:57
  • Thank you, I edited my post (and added one more Question) – AkiS Aug 04 '14 at 17:52
  • To ask a question, use the "Ask Question" button. – CL. Aug 04 '14 at 18:00

0 Answers0