[英]InvalidCastException: Cannot cast from source type to destination type. (Unity c# ,foreach list loop)
[英]InvalidCastException: Cannot Cast from source type to destination type. sqlite
我試圖從sqlite表中獲取數據,並統一使用它。 這是我的代碼
string conn = "URI=file:DB.db"; //Path to database.
IDbConnection dbconn;
dbconn = (IDbConnection)new SqliteConnection(conn);
try
{
dbconn.Open(); //Open connection to the database.
}
catch (Exception ex)
{
throw ex;
}
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT * FROM mytable WHERE id =" + LevelID + "";
dbcmd.CommandText = sqlQuery;
IDataReader reader = null;
reader = dbcmd.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
string question = reader.GetString(0);
string cat = reader.GetString(1);
}
}
我得到的異常:
InvalidCastException: Cannot cast from source type to destination type.
Mono.Data.Sqlite.SqliteDataReader.VerifyType (Int32 i, DbType typ)
Mono.Data.Sqlite.SqliteDataReader.GetString (Int32 i)
我已經檢查了數據庫,並且該字段是“ TEXT”,並且其中存儲的數據是一個字符串,並且我在聲明字符串,那么為什么它說它是Int呢?
避免使用SELECT * FROM mytable
但嘗試更明確地說明您選擇的內容。 例如,改為這樣編寫查詢:
SELECT NameOfColumn1, NameOfColumn2, ... FROM mytable
Select *
可能會Select *
比您期望的2列更多的列(如果表中有更多列,則會選擇所有列)。 因此,鑄件破裂。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.