code_junkie

F# error: Wrong number of parameters

Nov 13th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. let result = Seq.to_list(Microsoft.FSharp.Compatibility.Seq.generate_using opener generator)
  2.  
  3. let opener() =
  4. let command = connection.CreateCommand(CommandText = sql, CommandType = System.Data.CommandType.Text)
  5. command.ExecuteReader()
  6.  
  7. let generator<'a> (reader : System.Data.IDataReader) =
  8. if reader.Read() then
  9. let t = typeof<'a>
  10. let props = t.GetProperties()
  11. let types = props
  12. |> Seq.map (fun x -> x.PropertyType)
  13. |> Seq.to_array
  14. let cstr = t.GetConstructor(types)
  15. let values = Array.create reader.FieldCount (new obj())
  16. reader.GetValues(values) |> ignore
  17. let values = values
  18. |> Array.map (fun x -> match x with | :? System.DBNull -> null | _ -> x)
  19. Some (cstr.Invoke(values) :?> 'a)
  20. else
  21. None
  22.  
  23. #light
  24. open System.Data
  25. open System.Data.SqlClient
  26. open System.Configuration
  27.  
  28.  
  29. type Employee =
  30. { FirstName: string;
  31. LastName: string; }
  32.  
  33. let generator<'a> (reader: IDataReader) =
  34. if reader.Read() then
  35. let t = typeof<'a>
  36. let props = t.GetProperties()
  37. let types = props
  38. |> Seq.map (fun x -> x.PropertyType)
  39. |> Seq.to_array
  40. let cstr = t.GetConstructor(types)
  41. let values = Array.create reader.FieldCount (new obj())
  42. reader.GetValues(values) |> ignore
  43. let values = values
  44. |> Array.map (fun x -> match x with | :? System.DBNull -> null | _ -> x)
  45. Some (cstr.Invoke(values) :?> 'a)
  46. else
  47. None
  48.  
  49. let opener() =
  50. let sql = "select * from employees"
  51. let connection = new SqlConnection(ConfigurationManager.ConnectionStrings.["myConnection"].ConnectionString)
  52. let command = connection.CreateCommand(CommandText = sql, CommandType = System.Data.CommandType.Text)
  53. command.ExecuteReader()
  54.  
  55. let result = Seq.to_list(Microsoft.FSharp.Compatibility.Seq.generate_using opener (generator<Employee>))
Add Comment
Please, Sign In to add comment