Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let result = Seq.to_list(Microsoft.FSharp.Compatibility.Seq.generate_using opener generator)
- let opener() =
- let command = connection.CreateCommand(CommandText = sql, CommandType = System.Data.CommandType.Text)
- command.ExecuteReader()
- let generator<'a> (reader : System.Data.IDataReader) =
- if reader.Read() then
- let t = typeof<'a>
- let props = t.GetProperties()
- let types = props
- |> Seq.map (fun x -> x.PropertyType)
- |> Seq.to_array
- let cstr = t.GetConstructor(types)
- let values = Array.create reader.FieldCount (new obj())
- reader.GetValues(values) |> ignore
- let values = values
- |> Array.map (fun x -> match x with | :? System.DBNull -> null | _ -> x)
- Some (cstr.Invoke(values) :?> 'a)
- else
- None
- #light
- open System.Data
- open System.Data.SqlClient
- open System.Configuration
- type Employee =
- { FirstName: string;
- LastName: string; }
- let generator<'a> (reader: IDataReader) =
- if reader.Read() then
- let t = typeof<'a>
- let props = t.GetProperties()
- let types = props
- |> Seq.map (fun x -> x.PropertyType)
- |> Seq.to_array
- let cstr = t.GetConstructor(types)
- let values = Array.create reader.FieldCount (new obj())
- reader.GetValues(values) |> ignore
- let values = values
- |> Array.map (fun x -> match x with | :? System.DBNull -> null | _ -> x)
- Some (cstr.Invoke(values) :?> 'a)
- else
- None
- let opener() =
- let sql = "select * from employees"
- let connection = new SqlConnection(ConfigurationManager.ConnectionStrings.["myConnection"].ConnectionString)
- let command = connection.CreateCommand(CommandText = sql, CommandType = System.Data.CommandType.Text)
- command.ExecuteReader()
- let result = Seq.to_list(Microsoft.FSharp.Compatibility.Seq.generate_using opener (generator<Employee>))
Add Comment
Please, Sign In to add comment