C# 如何确定C中SQLite查询返回的列的所有者表

C# 如何确定C中SQLite查询返回的列的所有者表,c#,sqlite,system.data.sqlite,C#,Sqlite,System.data.sqlite,我正在使用System.Data.SQLite。我正在编写一个C程序,从任何给定的SQLite数据库读取数据。 这个程序在给定的数据库上运行许多查询。我查询sqlite_主表以确定数据库中的表,然后运行PRAGMA语句以获取每个表的模式 我的主要问题是;当我执行SELECT*FROM table_1连接table_2 ON table_1.id=table_2.id;我找不到确定每个id列来自哪个表的方法 我希望有人能为我指明方向,让我能够确定查询结果中每一列所来自的表 我已经能够在NameVa

我正在使用System.Data.SQLite。我正在编写一个C程序,从任何给定的SQLite数据库读取数据。 这个程序在给定的数据库上运行许多查询。我查询sqlite_主表以确定数据库中的表,然后运行PRAGMA语句以获取每个表的模式

我的主要问题是;当我执行SELECT*FROM table_1连接table_2 ON table_1.id=table_2.id;我找不到确定每个id列来自哪个表的方法

我希望有人能为我指明方向,让我能够确定查询结果中每一列所来自的表

我已经能够在NameValueCollection中获取数据,它不会以我希望的方式处理重复的列名。我想要一个字典,表名作为键,列字典作为值;示例如下:

{"table_1": {"col_1": value, "col_2": value}, "table_2": {"col_1": value, "col_2": value}}
或限定的列名,以便我可以访问以下项目:

reader["table_name.column_name"];
我还能够从DataTable中的查询中获取数据,而DataTable并没有以我希望的方式获取数据。上面示例中的id列只是附加了一个数字1,以指示它是重复的

我用于返回NameValueCollections和DataTable的两个函数如下:

class DatabaseReader
{
    private string ConnectionString;

    public DatabaseReader(string ConnectionString)
    {
        this.ConnectionString = ConnectionString;
    }

    /// <summary>
    /// Returns a NameValueCollection of the query result
    /// </summary>
    /// <param name="query">The query to be run</param>
    public IEnumerable<NameValueCollection> ExecuteReader(string query)
    {
        SQLiteCommand command = new SQLiteCommand();
        command.CommandText = query;

        using (SQLiteConnection conn = new SQLiteConnection(this.ConnectionString))
        {
            conn.Open();
            // Do things with the open connection
            command.Connection = conn;
            SQLiteDataReader reader;
            using (command)
            {
                reader = command.ExecuteReader();
            }

            if (reader != null)
            {
                while (reader.Read())
                {
                    yield return reader.GetValues();
                }
            }
            else
            {
                throw new Exception(string.Format("{0} query returned nothing"));
            }
        }
    }

    /// <summary>
    /// Returns a DataTable of the query result
    /// </summary>
    /// <param name="query">The query to be run</param>
    public DataTable GetDataTable(string query)
    {
        DataTable dt = new DataTable();
        try
        {
            using (SQLiteConnection cnn = new SQLiteConnection(this.ConnectionString))
            {
                cnn.Open();
                using (SQLiteCommand mycommand = new SQLiteCommand(cnn))
                {
                    mycommand.CommandText = query;
                    using (SQLiteDataReader reader = mycommand.ExecuteReader())
                    {
                        dt.Load(reader);
                        reader.Close();
                    }
                }
                cnn.Close();
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        return dt;
    }

}

非常感谢您的帮助!:

我认为问题出在您的SQL查询中。必须为要从表中检索的每个列设置名称。因此,您可以使用以下查询:

SELECT table_1.ID as Tbl1_ID, table_2.ID as Tbl2_ID FROM table_1 JOIN table_2 ON table_1.id = table_2.id;
现在,您可以通过名称读取每个列的值,例如:

reader["Tbl1_ID"];
或者是索引,比如:

reader[0];

这是一个很好的解决办法。但是,当我运行这个查询时,我不知道表中存在哪些列,这可以通过编程计算出来,我总是想要所有的列,因此我要执行SELECT*FROM。。。语句。这就是为什么不应该使用SELECT*。是的,如果我回到绘图板并在执行查询时去掉SELECT*@MichaelMurphy,可能这一切都会更好。结果将是简单的表,并且只包含列名称和列值。没有关于从何处检索数据的额外数据。因此,如果每次查询结果都不一样,我认为您应该更改数据库设计,使执行查询变得简单。我不认为我已经很清楚了。我不知道该工具将与之一起使用的数据库,因此使用了通用SQL语句,也不知道为什么我要查询sqlite_主表以获取数据库中存在的表的信息。今晚我将重新设计一些东西,看看是否可以删除通用语句。