简体   繁体   English

C#-从sqlite获取双精度时的InvalidCastException

[英]C# - InvalidCastException when fetching double from sqlite

I keep getting a InvalidCastException when I'm fetching any double from my SQLite database in C#. 当我从C#中的SQLite数据库中获取任何double时,我一直收到InvalidCastException。 The exception says "Specified cast is not valid." 异常说“指定的转换无效。”

I am able to see the value in a SQL manager so I know it exists. 我能够在SQL管理器中看到该值,所以我知道它存在。 It is possible to fetch Strings (VARCHARS) and ints from the database. 可以从数据库中获取字符串(VARCHARS)和整数。 I'm also able to fetch the value as an object but then I get "66.0" when it's suppose to be "66,8558604947586" (latitude coordination). 我也可以将值作为对象来获取,但是当我假设它是“ 66,8558604947586”(纬度协调)时,我得到“ 66.0”。

Any one who knows how to solve this? 谁知道如何解决这个问题?

My code: 我的代码:

using System.Data.SQLite;

...

SQLiteConnection conn = new SQLiteConnection(@"Data Source=C:\\database.sqlite;    Version=3;");
conn.Open();
SQLiteDataReader reader = getReader(conn, "SELECT * FROM table");

//These are working 
String name = reader.GetString(1);
Int32 value = reader.GetInt32(2);

//This is not working
Double latitude = reader.getDouble(3);

//This gives me wrong value
Object o = reader[3]; //or reader["latitude"] or reader.getValue(3)

You may want to check the data type you used to store the value in the database. 您可能需要检查用于将值存储在数据库中的数据类型。 It seems like you may be storing an integer but trying to retrieve it as a double. 好像你可以存储一个整数,但试图检索它作为一个双。

Is the actual table schema defined to use a DOUBLE? 是否将实际的表模式定义为使用DOUBLE? SQLite will do some translation since the DB engine is typeless. 由于数据库引擎是无类型的,因此SQLite将进行一些转换 If it is actually a SQL DOUBLE column, you could try a cast as well. 如果它实际上是SQL DOUBLE列,则也可以尝试强制类型转换。

More information on SQLite type affinity. 有关SQLite类型关联的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM