我正在构建一个附带SQLite数据库的C#桌面应用程序。当我在C#中运行一个简单的查询并将其存储在DataTable中时,它会提供与SQLite Expert不同的结果。预期的结果是SQLite专家。
涉及表格:
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);
表格数据:
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);
查询:
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提供以下结果(步骤和成本实际值无关):
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
但C#从.sql文件调用相同的查询会产生以下结果:
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;
}
有什么想法吗?
编辑:链接的线程有效(上面编辑过的C#代码)并感谢您的@CL