F#与DataReader一起使用 [英] F# working with DataReader

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

问题描述

 让阅读器= selectCommand.ExecuteReader()让getBytesData(x:IDataReader)=让len = reader.GetBytes(1,int64 0,null,0,0);//创建一个缓冲区来保存字节,然后//从DataTableReader中读取字节.let buffer:字节数组= Array.zeroCreate(int32 len)x.GetBytes(1,int64 0,buffer,0,int32 len)|>忽略缓冲让retVal =列出[而reader.Read()做产量(reader.GetString(0),getBytesData阅读器,reader.GetDateTime(2))] 

我上面有代码可以从datareader读取bytes [].

getBytesData函数获取读取器并从读取器返回bytes [].

  • 一切正常,但是getBytesData函数的工作方式非常不正常.
  • 我只创建零填充字节数组来创建数组,有没有办法创建动态扩展或固定长度的数组

我有什么方法可以在F#中进行优化?

抱歉,我已经在F#上启动了一个新项目,以将其中的所有汁液榨干,因此尝试使每条生产线以最佳方式获得

解决方案

IDataReader GetBytes 方法实际上并没有提供任何选项来将代码编写为更具功能性的方式(它需要修改一个数组,所以我们只需给它一些数组...).

因此,您的代码版本非常好-即使它不能完全正常运行,您也至少可以将命令性部分本地化为单个函数,并使程序的其余部分保持正常运行(这是一个很好的结果)!

我对您的代码所做的唯一更改是将 reader 移至序列理解(以使其更加本地化),并且将使用 use 关键字以确保正确处理它(而且,您在序列表达式中不需要 List 标识符):

 让retVal =[使用reader = selectCommand.ExecuteReader()而reader.Read()做产量(reader.GetString(0),getBytesData阅读器,reader.GetDateTime(2))] 

let reader = selectCommand.ExecuteReader()

let getBytesData (x : IDataReader) = 
    let len = reader.GetBytes(1, int64 0, null, 0, 0);
    // Create a buffer to hold the bytes, and then
    // read the bytes from the DataTableReader.
    let buffer : byte array = Array.zeroCreate (int32 len)
    x.GetBytes(1, int64 0, buffer, 0, int32 len) |> ignore
    buffer

let retVal = 
  List [ while reader.Read() do 
           yield (reader.GetString(0), getBytesData reader, 
                  reader.GetDateTime(2)) ]

I have above code to read bytes[] from datareader.

getBytesData function takes reader and returns bytes[] from reader.

  • everything works fine but it getBytesData function is working very non-functional way.
  • i am creates zero filled byte array just to create array, is there any way of creating dynamic expanding or fixed lenght array

Is there any way i can optimize in F#?

Sorry for kind of question, but i have started a new project on F# to squeeze all juice out of it, so trying to get each line most optimal way

解决方案

The GetBytes method of the IDataReader doesn't really provide any options for writing the code in a more functional way (it takes an array that it wants to modify, so we simply must give it some array...).

So your version of code is perfectly fine - even though it's not fully functional, you can at least keep the imperative part localized in that single function and keep the rest of your program functional (which is a good result)!

The only change I would do in your code is that I would move reader to the sequence comprehension (to make it more localized) and I would use the use keyword to make sure that it gets properly disposed (also, you don't need the List identifier in the sequence expression):

let retVal =  
  [ use reader = selectCommand.ExecuteReader() 
    while reader.Read() do  
      yield (reader.GetString(0), getBytesData reader,  reader.GetDateTime(2)) ]

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

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