In C# I use sql scripts to add data into a List where T would be a class with fields/properties mapped to the sql script.
How could I do this in F#? This piece uses a stored procedure in the standard way.
using (conn)
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("dbo.query_here", conn))
{
cmd.CommandText = "dbo.query_here";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandTimeout = 600;
cmd.Parameters.Add(new SqlParameter("@x1", Convert.ToString(x)));
cmd.Parameters.Add(new SqlParameter("@y1", y));
cmd.Parameters.Add(new SqlParameter("@z1", z));
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MyListOfClasses.Add(new MyDataClass(reader.GetInt32(reader.GetOrdinal("x"))
reader.GetDouble(reader.GetOrdinal("y")),
reader.GetDouble(reader.GetOrdinal("a")),
reader.GetDouble(reader.GetOrdinal("b"))));
}
reader.Close();
}
conn.Close();
I realise F# is not quite as straight forward perhaps as this, but I need to get this data into F# lists in a similar way. Also would prefer suggestions that were not functional in nature and followed a similar pattern to C# code.
In a previous thread someone suggested using records, but still this was not in relation to SqlDataReader. It would be preferable to have a list of classes so I can use getters and setters on each item.
I should add before the inevitable comments of "why not just use C#". Well obviously I can use C#, but I am exploring possibilities of writing algorithms in F# and to do that I need to get my raw data from SQL Server.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…