I got a problem where I can't read the data from my database correctly.
To give you an example, I got the important value that is in the database 6.69879289850025E-06, but I read 6.0 (which is not accurate since it's suppose to be way smaller) in the C# program.
Something similar happen to ImportantValue2 where the value that is in the database is -0,000158976621370616 and in my C# program I get 0,0.
public double ImportantValue1 { get; set; }
public double ImportantValue2 { get; set; }
public string Note { get; set; }
public MyObject(SQLiteDataReader reader)
{
ImportantValue1 = Convert.ToDouble(reader["important_value_1"]); //Value in the database is REAL
ImportantValue2 = Convert.ToDouble(reader["important_value_2"]);//Value in the database is REAL
Note = reader["note"].ToString(); //Value in the database is TEXT
}
Update
And this is how I call it.
using (SQLiteConnection c = new SQLiteConnection(connection))
{
c.Open();
using (SQLiteCommand cmd = new SQLiteCommand(sqlCommand, c))
{
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
objectFromBD = new MyObject(reader);
}
}
}
}
And the SQLite code
CREATE TABLE "table"(
"id" INTEGER,
"important_value_1" REAL NOT NULL,
"important_value_2" REAL NOT NULL,
"note" TEXT NOT NULL,
PRIMARY KEY ("id" AUTOINCREMENT)
);
Thank you for your help!
CodePudding user response:
reader["important_value_1"]
will return index which column exists in your SQL script instead of your expectation value.
reader["important_value_1"]
will get the value of the specified column.
You can try to use reader["important_value_1"]
in reader.GetDouble
function to read your data from columns.
ImportantValue1 = reader.GetDouble(reader["important_value_1"])
ImportantValue2 = reader.GetDouble(reader["important_value_2"])
SqliteDataReader.Item[] Property
Note
You might need to use reader.Read()
let the reader point to read the next row if you want.
while (reader.Read()){
//... read all rows from your db
}
CodePudding user response:
Here is a temporary solution I got, but that is not efficient.
I saw that I can read a string with the reader, but I couldn't do it with a double. So I used reader.GetString(rowValue);
and than I converted the string to a double using the fonction below.
public double StringToDoubleWithNotation(string value)
{
return Convert.ToDouble(Decimal.Parse(value, NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint));
}
Now everything is fine, but I would prefer to see a better solution since doing 3 conversion is not that efficient.