在F#中使用SqlDataReader [英] Use SqlDataReader in F#

查看:100
本文介绍了在F#中使用SqlDataReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我使用sql脚本将数据添加到列表中,其中T将是一个具有映射到sql脚本的字段/属性的类.

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.

我该如何在F#中执行此操作?这篇文章以标准方式使用存储过程.

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();

我意识到F#可能不那么简单,但是我需要以类似的方式将这些数据放入F#列表中.同样,他们更喜欢本质上不起作用的建议,并且遵循与C#代码类似的模式.

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.

在以前的线程中,有人建议使用记录,但这仍然与SqlDataReader无关.最好有一个类列表,以便我可以在每个项目上使用getter和setter.

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.

我应该在不可避免的注释之前加上为什么不仅仅使用C#".显然,我可以使用C#,但是我正在探索用F#编写算法的可能性,为此,我需要从SQL Server中获取原始数据.

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.

推荐答案

如果您想在F#列表中返回results,则列表理解适用于此.并且使用use关键字,您无需显式处置对象.

If you would like to return results in F# list, list comprehension is suitable to use here. And with use keyword, you don't need to explicitly dispose objects.

use conn = (* Initialize sql connection here *)
conn.Open()
use 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))
use reader = cmd.ExecuteReader()
let results =
 [ while reader.Read() do
    yield new MyDataClass(reader.GetInt32(reader.GetOrdinal("x")),           
                          reader.GetDouble(reader.GetOrdinal("y")),
                          reader.GetDouble(reader.GetOrdinal("a")),
                          reader.GetDouble(reader.GetOrdinal("b"))) ]

这篇关于在F#中使用SqlDataReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆