[英]SQLite ExecuteReader --> DataTable.Load --> FormatException (DateTime)
[英]DataTable.Load() not getting data from ExecuteReader()
我正在 Unity 中使用 SQLite,並且在從游戲在開始屏幕生成的表中加載數據時遇到問題。 我沒有收到任何錯誤,我的“reader.HasRows”調用確實恢復原狀。 但是,當我嘗試將數據加載到 DataTable 中時,我一無所獲。 我之前在 C# 中使用過 SQLite,並且正在使用該代碼作為參考,但我以前從未遇到過這樣的問題。
ScoreControl.cs:
using (SqliteConnection dbCon = new SqliteConnection("Data Source=" + Application.dataPath + "MainScoreDB.sqlite3"))
{
if (dbCon.State != ConnectionState.Open)
dbCon.Open();
string query = "SELECT * FROM highscores";
SqliteCommand command = new SqliteCommand(query, dbCon);
SqliteDataReader reader = command.ExecuteReader();
if(reader.HasRows)
{
Scores.Load(reader);
}
else
{
Debug.Log("No Data was Returned");
}
if (dbCon.State == ConnectionState.Open)
dbCon.Close();
}
else 語句永遠不會被觸發,所以讀者肯定會得到一些東西。 我在這里不知所措,我嘗試了多種格式的數據庫(s3db、sqlite、sqlite3),但結果都一樣。 我也沒有錯誤。
編輯
Application.dataPath 指的是根項目文件夾,我用它來生成數據庫和表,以及引用它,所以我知道它正在查詢正確的數據庫,我還制作了一個沒有表的數據庫,出現錯誤,所以我知道它正在訪問數據庫。
Scores.Load() 是它加載的 DataTable 中的一個方法,它輸入從讀取器返回的數據。
所以看起來 while 循環經歷了正確的次數,但它沒有返回任何東西。 我是否需要管理來自數據庫的數據類型?
我認為您忘記在數據讀取器上調用 Read。 光標在讀取第一行之前位於開頭。 嘗試執行 while 循環以遍歷所有行,因為 Reader 是僅向前移動的對象。
if (reader.HasRows)
{
while (reader.Read())
{
// Load each row 1 at a time here
}
}
參考:
編輯
嘗試使用數據適配器,看看它是否表現得更好,或者為您提供了另一個錯誤線索。
SQLiteDataAdapter da = new SQLiteDataAdapter(dbCommand);
da.Fill(scores);
我遇到了類似的問題,
除了我的是,出於某種原因,DataTable 根本沒有收到它所欠的所有行。
為 SqlLite 填充 DataTable 的正確方法似乎是:
DataTable dt = new DataTable();
SQLiteConnection cnn = new SQLiteConnection(dbConnection);
cnn.Open();
SQLiteCommand mycommand = new SQLiteCommand(cnn);
mycommand.CommandText = sql;
SQLiteDataAdapter adapter = new SQLiteDataAdapter(mycommand);
adapter.Fill(dt);
cnn.Close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.