C
C#12mo ago
darck

Crazy database results I can't manage to debug.

Hi, I'm force by a college professor, for an assignement to do integration testing of a mysql database using sqlite. So I'm using the IDbConnection, IDbCommand and IDataReader interfaces from ADO.NET to make it work with whatever. After dealing with datetime and timestamp issues, due to the differences of the sql dialects, I'm running into into a verry annoying issue where SQLite doesn't let you specify the length of integers you want (no need to do so according to it's documentation as it adapst by itself depending based on the length of the value you insert).
int id = (int)(long)reader["id"]; //reader is an IDataReader that has the results from a querry ran with IDbCommand.ExecuteReader()

System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Int64'.
at Stas.Monitor.Infrastructures.ThermometresRepository.GetThermometres() in /home/mathieub/Documents/b2/ai/Stas.Monitor/Stas.Monitor.Infrastructures/ThermometresRepository.cs:line 27
at Stas.Monitor.App.App.OnFrameworkInitializationCompleted() in /home/mathieub/Documents/b2/ai/Stas.Monitor/Stas.Monitor.App/App.axaml.cs:line 58
int id = (int)(long)reader["id"]; //reader is an IDataReader that has the results from a querry ran with IDbCommand.ExecuteReader()

System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Int64'.
at Stas.Monitor.Infrastructures.ThermometresRepository.GetThermometres() in /home/mathieub/Documents/b2/ai/Stas.Monitor/Stas.Monitor.Infrastructures/ThermometresRepository.cs:line 27
at Stas.Monitor.App.App.OnFrameworkInitializationCompleted() in /home/mathieub/Documents/b2/ai/Stas.Monitor/Stas.Monitor.App/App.axaml.cs:line 58
the problem is that if I don't do this then sometimes sqlite decides it's gonna be a System.Int64 and I therefore need to cast it into a long before casting it into an int. I don't know why sqlite does this as every value I ever insert into the database is under the 2 billion something max value for a signed int on 32bits. Is there a way, without knowing the type before runtime to just makes whatever type of int its going to be into an System.Int32 regardless of the number of bits it was originally stored on. (reader can be acessed as a dictionnary of objects) ?
5 Replies
Buddy
Buddy12mo ago
Please use the GetX() methods
darck
darckOP12mo ago
int id;
object objId = reader["id"];
if (objId is long)
{
id = (int)(long)objId;
}
else
{
id = (int)objId;
}
int id;
object objId = reader["id"];
if (objId is long)
{
id = (int)(long)objId;
}
else
{
id = (int)objId;
}
sorry for the formatting, this is probably not great but it seems to dot he trick, I haven't found anything when typing GetX into my IDE and when googling (GetX() methods C#) it get java related things.
ThatDaniel
ThatDaniel12mo ago
IDataReader Interface (System.Data)
Provides a means of reading one or more forward-only streams of result sets obtained by executing a command at a data source, and is implemented by .NET data providers that access relational databases.
ThatDaniel
ThatDaniel12mo ago
There's GetX() methods on the IDataReader to get the right type
darck
darckOP12mo ago
oh thanks I might tell that to my college teacher, after I'm done with the assignement that is
Want results from more Discord servers?
Add your server