Sqllite SQLiteDataReader返回空读取器,而SQLiteDataAdapter返回正确的结果

时间:2018-06-28 21:20:59

标签: sqlite

我在使用Sqllite SQLiteDataReader时遇到麻烦。使用相同的连接字符串和相同的sql语句SQLiteDataReader返回空的阅读器,而SQLiteDataAdapter返回可疑的记录。

在这种情况下,我尝试获取ID字段中具有最高值的记录。 该数据库包含多个在ID字段中具有唯一值的记录,但是使用SQLiteDataReader时,读者返回为空。当我将相同的连接字符串和sql语句与SQLiteDataAdapter一起使用时,会出现可疑的结果。我提供了用于与数据库通信的静态类的一部分。使用SQLiteDataReader的SenasteBokning方法不起作用。使用SQLiteDataAdapter的SenasteBokning2方法可以完美工作。 SenasteBokning方法有什么问题?

我使用: Windows 10 Visual Studio 2017 .net Framework 4.5.2(在创建Windows Forms应用程序时为默认值) Nuget包System.Data.SQLite.Core 1.0.108

static class Databas
{
    private static string appPath =     Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    private static string dbPath = @"\BokningarConvention.db";
    private static string connectionString = "Data Source= " + appPath + dbPath;
    private static SQLiteConnection con;
    private static SQLiteCommand cmd;
    private static SQLiteDataReader reader;
    private static SQLiteDataAdapter adapter;
    private static SQLiteCommandBuilder commandBuilder;
    private static DataTable table; 
    private static string senaste = "SELECT Nummer, NrSammaDag, Datum FROM Bekraftelser WHERE Id = (SELECT MAX (Id) FROM  Bekraftelser)";

    // This don't work
    public static Bokning SenasteBokning()
    {
        Bokning bokning = new Bokning();

        using (SQLiteConnection con2 = new SQLiteConnection(connectionString))
        {
            con2.Open();
            SQLiteCommand cmd2 = new SQLiteCommand(senaste, con2);

            SQLiteDataReader reader2 = cmd2.ExecuteReader();

            // Here the reader is empty
            while (reader2.Read())
            {
                // Error at first read
                // should handle results the same way as in SenasteBokning2
                // removed during testing
            }
        }

        return bokning;
    }


    //This works perfekt
    public static Bokning SenasteBokning2()
    {
        Bokning bokning = new Bokning();
        using (SQLiteConnection db = new SQLiteConnection(connectionString))
        {
            adapter = new SQLiteDataAdapter(senaste, connectionString);
            commandBuilder = new SQLiteCommandBuilder(adapter);
            table = new DataTable();
            db.Open();
            adapter.Fill(table);

            foreach (DataRow row in table.Rows)
            {
                int nummer;
                int samma;
                DateTime datum;
                nummer = (int)((long)row["Nummer"]);
                datum = Verktyg.FromDateInteger((int)((long)row["Datum"]));

                if (!row.IsNull("NrSammaDag"))
                {
                    samma = (int)((long)row["NrSammaDag"]);
                    bokning = new Bokning(nummer, samma, datum);
                }
                else
                {
                    bokning = new Bokning(nummer, datum);
                } 
            }
        }
        return bokning;           
    }        
}

0 个答案:

没有答案