【发布时间】:2017-03-27 12:00:58
【问题描述】:
我试图用我自己的类型填充列表。
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 添加到列表中?还是从数据库中获取某种包含数据的列表的更好方法?
【问题讨论】:
-
如果您已经可以从数据库中获取您需要的列表,那可能是最好的方法。除此之外,您可以使用
ResizeArray<_>,即.NET 的System.Collections.Generic.List<_>。usersList当前是一个数组,在 .NET 中永远无法添加数组。 -
您访问的是什么数据库,是否有使用 ADO 的特定原因?您应该尝试使用类型提供程序访问它。无论哪种方式,尝试取回记录的序列(IEnumerable)而不是数组。