sqlite - Sqllite SQLiteDataReader 返回 enpty reader 而 SQLiteDataAdapter 返回正确的结果
问题描述
我在使用 Sqllite SQLiteDataReader 时遇到问题。使用相同的连接字符串和相同的 sql 语句 SQLiteDataReader 返回空读取器,而 SQLiteDataAdapter 返回可疑记录。
在这种情况下,我尝试在 Id 字段中获取具有最高值的记录。数据库包含几条Id字段中具有唯一值的记录,但使用SQLiteDataReader时读取器返回为空。当我使用与 SQLiteDataAdapter 相同的连接字符串和 sql 语句时,会出现可疑结果。我提供了用于与数据库通信的静态类的一部分。使用 SQLiteDataReader 的 SenasteBokning 方法不起作用。使用 SQLiteDataAdapter 的方法 SenasteBokning2 效果很好。SenasteBokning 方法有什么问题?
我使用:Windows 10 Visual Studio 2017 .net framework 4.5.2(在创建 Windows 窗体应用程序时是默认设置)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;
}
}
解决方案
推荐阅读
- reactjs - prevState 在 componentDidUpdate 中显示更新状态
- c - k&r 中的“entab”锻炼计划
- amazon-cloudfront - 使用 Cloudfront 提供 HTTPS 但支持 HTTP 源
- javascript - 变量分配和范围
- c++ - 为什么 QXmlQuery 似乎在结果中添加了一个`\n`?(以及如何解决?)
- python - python中的Paser XML
- html - CSS - 将文本与 div 对齐
- java - Java 多个键直接指向相同的值
- swift - 如何修复 GPUImage2 框架在从后台应用程序中删除时导致错误?
- sql - 将 MS SQL 服务器与 Google Data Studio 连接的最佳方式是什么?