【发布时间】:2017-05-20 18:41:01
【问题描述】:
我正在尝试从 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 呢?
【问题讨论】: