C#应用程序在将SQLite查询结果添加到List<;字符串>;

本文关键字:List gt lt 字符串 添加 应用程序 SQLite 结果 查询 | 更新日期: 2023-09-27 18:21:14

我有一个返回66行的查询,每行都包含一个unique_id,我想将结果保存在列表中,以便可以使用它们进行进一步的查询。但是代码冻结在本应将行添加到列表的行。我找不到我的问题在哪里!这是我的代码:

public List<string> unique_id;
public List<string> get_unique_id_connection()
    {
        SQLiteConnection m_dbConnection;
        m_dbConnection = new SQLiteConnection("Data Source=akm.sqlite;version=3;");
        m_dbConnection.Open();
        string sql2 = "SELECT unique_id FROM mos_tbl WHERE region = 10";
        SQLiteCommand command2 = new SQLiteCommand(sql2, m_dbConnection);
        SQLiteDataReader rdr2 = null;
        rdr2 = command2.ExecuteReader();
        while (rdr2.Read())
        {
            for (int i = 0; i < rdr2.FieldCount; i++)
            {
                //MessageBox.Show(rdr2.GetValue(i).ToString());
                unique_id.Add(rdr2.GetValue(i).ToString());//This is where it freezes
            }
        }
        return unique_id;
    }

当我取消注释MessageBox时,我可以看到正确的结果,所以我想问题不在于查询。

C#应用程序在将SQLite查询结果添加到List<;字符串>;

您确定没有抛出NullReferenceException吗?在添加元素时,似乎没有创建unique_id列表对象。