将 Sequence.Generate 转换为序列表达式

发布于 2024-10-31 01:14:44 字数 2160 浏览 4 评论 0原文

我有以下代码,它使用 Sequence 对象从数据库表中读取数据。 V1 工作正常,但由于 Seq.generate 函数已弃用,我收到编译器警告。

我尝试用 V2 替换此代码(如下),但它无法正常工作 - 基本上,阅读器已打开,但仅读取第一条记录 - IOW - 读取下一条不会 正常工作。

为了避免编译错误/警告,我需要将 V1 代码转换为使用序列表达式。

关于正确方法的任何想法都在这里。

(顺便说一句 - 该代码基于 Rob Pickering 的 Beginning F# 书中的示例 - (数据访问/ADO.NET 部分)。

********************** V1 - Sequence Approach - Deprecated ************************

// execute a command using the Seq.generate
let execCommand (connName: string) (cmdString: string) = 
    Seq.generate       
        // This function gets called to open a connection and create a reader
        (fun () -> openReader connName cmdString)
        // This function gets called to read a single item in
        // the enumerable for a reader/connection pair
        (fun reader -> readRow(reader))
        (fun reader -> reader.Dispose())

*********************** V2  Alternative - (Does not work) ***************************

let generateSequence connName cmdString =
       seq { // This function gets called to open a connection and
              //create a reader
              use reader = openReader connName cmdString
              // This function gets called to read a single item in
               // the enumerable for a reader/connection pair
              yield readRow(reader) }

// execute a command using the Seq.generate
let execCommand (connName: string) (cmdString: string) = 

    generateSequence connName cmdString


*****************************  Common Functions  **********************************

// Open a db connection
let openReader connName cmdString =
    let conn = openSQLConnection(connName)
    let cmd = conn.CreateCommand(CommandText=cmdString,
                                 CommandType = CommandType.Text)
    let reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

// read a row from the data reader
let readRow (reader: #DbDataReader) =
    if reader.Read() then
        let dict = new Dictionary<string, obj>()
        for x in [ 0 .. (reader.FieldCount - 1) ] do
            dict.Add(reader.GetName(x), reader.[x])
        Some(dict)
    else
        None

I have the following code that uses Sequence objects to read data from a database table.
V1 works correctly but since the Seq.generate function is deprecated I receive compiler warnings.

I tried to rerplace this code with V2 (below) but it does not work correctly -
basically the reader is opened but only the first record is read - IOW - Read Next does not
work correctly.

To avoid compilation errors/warnings I need to convert V1 code to use a sequence expression.

Any ideas on the correct approach here.

(BTW - This code is based on examples in Rob Pickering's Beginning F# book -
(Data Access/ADO.NET Section).

********************** V1 - Sequence Approach - Deprecated ************************

// execute a command using the Seq.generate
let execCommand (connName: string) (cmdString: string) = 
    Seq.generate       
        // This function gets called to open a connection and create a reader
        (fun () -> openReader connName cmdString)
        // This function gets called to read a single item in
        // the enumerable for a reader/connection pair
        (fun reader -> readRow(reader))
        (fun reader -> reader.Dispose())

*********************** V2  Alternative - (Does not work) ***************************

let generateSequence connName cmdString =
       seq { // This function gets called to open a connection and
              //create a reader
              use reader = openReader connName cmdString
              // This function gets called to read a single item in
               // the enumerable for a reader/connection pair
              yield readRow(reader) }

// execute a command using the Seq.generate
let execCommand (connName: string) (cmdString: string) = 

    generateSequence connName cmdString


*****************************  Common Functions  **********************************

// Open a db connection
let openReader connName cmdString =
    let conn = openSQLConnection(connName)
    let cmd = conn.CreateCommand(CommandText=cmdString,
                                 CommandType = CommandType.Text)
    let reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

// read a row from the data reader
let readRow (reader: #DbDataReader) =
    if reader.Read() then
        let dict = new Dictionary<string, obj>()
        for x in [ 0 .. (reader.FieldCount - 1) ] do
            dict.Add(reader.GetName(x), reader.[x])
        Some(dict)
    else
        None

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

于我来说 2024-11-07 01:14:44

您需要移动一些控制结构才能使其正常工作。

let generateSequence connName cmdString =
    seq { 
        use reader = openReader connName cmdString
        while reader.Read () do
            yield readRow reader
        yield None
    }

let readRow (reader:#DbDataReader) =
    let dict = new Dictionary<string, obj>()
    for x in [0..(reader.FieldCount - 1)] do
        dict.Add (reader.GetName(x), reader.[x])
    Some dict

You need to move around a few control structures to get this working.

let generateSequence connName cmdString =
    seq { 
        use reader = openReader connName cmdString
        while reader.Read () do
            yield readRow reader
        yield None
    }

let readRow (reader:#DbDataReader) =
    let dict = new Dictionary<string, obj>()
    for x in [0..(reader.FieldCount - 1)] do
        dict.Add (reader.GetName(x), reader.[x])
    Some dict
余罪 2024-11-07 01:14:44

您唯一遗漏的是:while reader.Read() do

let generateSequence connName cmdString =
    seq { 
        use reader = openReader connName cmdString
        while reader.Read() do
            yield readRow reader 
    }

The only thing you left out is: while reader.Read() do

let generateSequence connName cmdString =
    seq { 
        use reader = openReader connName cmdString
        while reader.Read() do
            yield readRow reader 
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文