2016-11-13 62 views
1

我試圖用我自己的類型填充列表。使用類型填充列表

let getUsers = 
    use connection = openConnection() 
    let getString = "select * from Accounts" 
    use sqlCommand = new SqlCommand(getString, connection) 
    try 
     let usersList = [||] 
     use reader = sqlCommand.ExecuteReader() 
     while reader.Read() do 
      let floresID = reader.GetString 0 
      let exName = reader.GetString 1 
      let exPass = reader.GetString 2 
      let user = [floresID=floresID; exName=exName; exPass=exPass] 
      // what here? 
      () 
    with 
     | :? SqlException as e -> printfn "Došlo k chybě úrovni připojení:\n %s" e.Message 
     | _ -> printfn "Neznámá výjimka." 

在C#我只想補充新的對象爲userList。我如何將新的user添加到列表中?或者更好的方法來從數據庫中獲取某種列表的數據?

+3

如果您已經可以從數據庫中獲取所需的列表,那可能是最好的方法。除此之外,您可以使用'ResizeArray <_>',這是.NET的'System.Collections.Generic.List <_>'。 'usersList'目前是一個數組,並且數組永遠不會被添加到.NET中。 – TeaDrivenDev

+0

您訪問了哪個數據庫,是否有使用ADO的具體原因?您應該嘗試使用類型提供程序訪問它。無論哪種方式,而不是一個數組嘗試找回記錄的seq(IEnumerable)。 – s952163

回答

2

最簡單的方法是使用類型提供程序,因此可以抽象出數據庫。對於SQLServer,您可以使用SqlDataConnection,對於所有內容(包括SQLServer),還可以使用SqlProvider,對於SQLServer也可以使用。

這裏是postgres的的dvdrental作爲(樣品)數據庫sqlProvider的一個例子:

#r @"..\packages\SQLProvider.1.0.33\lib\FSharp.Data.SqlProvider.dll" 
#r @"..\packages\Npgsql.3.1.8\lib\net451\Npgsql.dll" 

open System 
open FSharp.Data.Sql 
open Npgsql 
open NpgsqlTypes 
open System.Linq 
open System.Xml 
open System.IO 
open System.Data 

let [<Literal>] dbVendor = Common.DatabaseProviderTypes.POSTGRESQL 
let [<Literal>] connString1 = @"Server=localhost;Database=dvdrental;User Id=postgres;Password=root" 
let [<Literal>] resPath = @"C:\Users\userName\Documents\Visual Studio 2015\Projects\Postgre2\packages\Npgsql.3.1.8\lib\net451" 
let [<Literal>] indivAmount = 1000 
let [<Literal>] useOptTypes = true 

//create the type for the database, based on the connection string, etc. parameters 
type sql = SqlDataProvider<dbVendor,connString1,"",resPath,indivAmount,useOptTypes> 
//set up the datacontext, ideally you would use `use` here :-) 
let ctx = sql.GetDataContext() 
let actorTbl = ctx.Public.Actor //alias the table 

//set up the type, in this case Records: 
type ActorName = { 
    firstName:string 
    lastName:string} 

//extract the data with a query expression, this gives you type safety and intellisense over SQL (but also see the SqlClient type provider above): 
let qry = query { 
      for row in actorTbl do 
      select ({firstName=row.FirstName;lastName=row.LastName}) 
       } 
//seq is lazy so do all kinds of transformations if necessary then manifest it into a list or array: 
qry |> Seq.toArray 

的兩個重要部分所定義的演員記錄,然後在查詢中提取的字段成的演員記錄的序列。如有必要,您可以將其列入列表或數組中。

但你也可以堅持你原來的解決方案。在這種情況下,只是包裝的.Read()seq

首先定義類型:

type User = { 
    floresID: string 
    exName: string 
    exPass: string 
} 

然後將解壓後的數據:

let recs = cmd.ExecuteReader() // execute the SQL Command 
//extract the users into a sequence of records: 
let users = 
    seq { 
     while recs.Read() do 
      yield {floresID=recs.[0].ToString() 
        exName=recs.[1].ToString() 
        exPass=recs.[2].ToString() 
        } 
     } |> Seq.toArray 
1

以你的代碼,你可以用列表表達:

let getUsers = 
    use connection = openConnection() 
    let getString = "select * from Accounts" 
    use sqlCommand = new SqlCommand(getString, connection) 
    try 
     [ 
      use reader = sqlCommand.ExecuteReader() 
      while reader.Read() do 
       let floresID = reader.GetString 0 
       let exName = reader.GetString 1 
       let exPass = reader.GetString 2 
       let user = [floresID=floresID; exName=exName; exPass=exPass] 
       yield user 
     ] 
    with 
     | :? SqlException as e -> failwithf "Došlo k chybě úrovni připojení:\n %s" e.Message 
     | _ -> failwithf "Neznámá výjimka." 

這就是說,我會使用FSharp.Data.SqlClient libra所以所有的鍋爐板都變成了一條單線,並增加了類型安全性的好處(如果你改變了查詢,代碼將會有編譯時錯誤,這些錯誤很明顯可以解決)。