C#SQL-在DataGridView中加载DataTable会更改SQL查询';s数据

本文关键字:查询 SQL 数据 DataGridView 加载 DataTable C#SQL- | 更新日期: 2023-09-27 17:57:25

我正在用C#为一个跨国计时器项目制作一个SQLite查询生成器。它根据您在组合框中选择的信息生成查询。

从我的项目一开始,我就一直在使用以下功能

private static DataTable ExecuteTableQuery(String query)
{
    DataTable dt = new DataTable();
    using (SQLiteCommand cmd = new SQLiteCommand(query, CONNECTION))
    {
        SQLiteDataReader reader = cmd.ExecuteReader();
        dt.Load(reader);
        return dt;
    }
}

以便设置我的DataGridView窗体.的DataSource

但是,我生成的查询越复杂,我就越开始遇到奇怪的情况。

查询示例:

SELECT "No. Arrived", "Status", "Time", "Time Total", "No. Runner" // [A]
FROM (
    SELECT `Runner_no` AS "No. Runner", // Runner_no is a PK
           `Result_time` AS "Time",
           `Result_totaltime` AS "Time Total",
           `Result_status` AS "Status",
           `Result_registered` AS "No. Arrived" // Result_registered is a PK
           // [B]
    FROM `Result`, `Runner`, `City`
    WHERE `Runner_City_id` = `City_id` // City_id is a PK
        AND `Result_id` = `Runner_no`
        AND (`Runner_gender` = "F" OR `Runner_gender` = "M")
        AND (`Result_status` = "hurt" OR `Result_status` = "disq" OR `Result_status` = "OK" )
    UNION // Some people may have not reached the finish line.
    SELECT `Runner_no` AS "No. Runner",
           NULL AS "Time",
           NULL AS "Time Total",
           NULL AS "Status",
           NULL AS "No. Arrived"
    FROM `Result`, `Runner`, `City`
    WHERE `Runner_City_id` = `City_id`
        AND `Runner_no` NOT IN (SELECT `Result_id` FROM `Result` )
        AND (`Runner_gender` = "F" OR `Runner_gender` = "M" )
    GROUP BY "Runner_no"
)
ORDER BY "No. Arrived" ASC

请注意,我的应用程序是用来生成变量SQL查询的,所以我的示例看起来很简单是正常的。

我的问题:大多数时候,(我认为)当我生成的查询在某些子句中具有主键时(比如在我的示例中),某些结果会被省略,排序会被忽略,有时,在[A]中选择另一行可能会使查询重新工作。

在我的例子中:No. Arrived列没有得到排序,一些行消失了,将列City_id添加到[A]和[B]中解决了所有问题。

注意:我的应用程序生成的任何查询都没有任何错误。在任何外部SQLite软件中执行它们(例如。http://sqlitebrowser.org/)总是成功的。

我的问题:当我在DataTable中加载数据时,为什么会发生这种情况?我该如何避免这种情况?如何在C#中获得与使用基于C++的软件(如SQLiteBrowser)相同的结果?

C#SQL-在DataGridView中加载DataTable会更改SQL查询';s数据

我得到了答案!谢谢你的帮助。似乎(在我的情况下)我不能使用SQLiteCommand,因为它在阅读时会省略一些基本的SQL规则(不确定具体是哪种),以减少过热。

对于像我这样的复杂查询,使用SQLiteDataAdapter可以很好地工作。

private static DataTable ExecuteTableQuery(String Query)
{
    using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(Query, CONNECTION))
    {
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        return dt;
    }
}

我发现union语句中的列与[A]中的列不匹配

SELECT `Runner_no` AS "No. Runner",
       NULL AS "Time",
       NULL AS "Time Total",
       NULL AS "Status",
       NULL AS "No. Arrived"

这是自然的吗?

或者可能是:

 SELECT  NULL AS "No. Arrived" ,
     NULL AS "Status",
     NULL AS "Time",
     NULL AS "Time Total",
    `Runner_no` AS "No. Runner"   

如果您确信sql语句给出了预期的结果,那么问题可能出现在DataTble Loading:中

我建议:

-给DataTble一个名称:

DataTable dt=新的DataTable("MyTable");

在外部SQL中使用不带空格或点的别名[A]:

选择"No Arrived"作为No_Arrived,选择"Status"、"Time"、"Total Time"作为Time_Total,选择"No Runner"作为No_Runner--[A]

顺便说一句,如果在外部sql[A]中使用别名,则不需要在内部sql中使用别名