I have created a SQL table with below code:
CREATE TABLE AAPL_1day
(
[INDEX] INT IDENTITY(1,1) PRIMARY KEY,
[DateTime] SMALLDATETIME NOT NULL,
[MonthNr] FLOAT NOT NULL,
[DayNr] FLOAT NOT NULL,
[roc_1] FLOAT,
[roc_2] FLOAT,
[hypo_upordown7days] FLOAT
);
First rows in the table looks like this:
The problem I have now is to load this table into the variable "data" below.
I use var preview = data.Preview();
to be able to see if there is any data loaded into "data". However var preview
is null.
I get the error below when running the code:
System.InvalidCastException: 'Specified cast is not valid.'
All columns in databaseName: AAPL have the datatype FLOAT. In the class "AAPLData" I have assigned "float" to all columns.
I can't figure out what I am doing wrong. I connect to the database and table successfully with the connection string and table name.
Even if I try to only retrieve one column, for example the below, I still get the same error:
[LoadColumn(2)] public float MonthNr;
Code:
void loadInData()
{
// Set up MLContext
var mlContext = new MLContext();
// Define database connection parameters
string databaseName = "AAPL";
string connectionString = "Server=localhost;Database=" + databaseName + ";Trusted_Connection=True;";
var tableName = "AAPL_1day";
var loader = mlContext.Data.CreateDatabaseLoader<AAPLData>();
var source = new DatabaseSource(SqlClientFactory.Instance, connectionString, $"SELECT * FROM {tableName}");
var data = loader.Load(source);
var preview = data.Preview(); //System.InvalidCastException: 'Specified cast is not valid.'
string dummy_breakpoint = ""; //Break point here
}
public class AAPLData
{
[LoadColumn(2)] public float MonthNr;
[LoadColumn(3)] public float DayNr;
[LoadColumn(4)] public float roc_1;
[LoadColumn(5)] public float roc_2;
[LoadColumn(6), ColumnName("Label")] public float hypo_upordown7days;
}
According to the following SO answer, a SQL float
is actually a double
, so the data type coming back is a double
and you'd need to convert it to a float
.