【发布时间】:2013-12-13 11:43:27
【问题描述】:
我有以下代码从 SQL 数据库中获取一行。它返回记录或无。但是,它没有返回,因为 getLogin 的类型为 unit = () 并且有警告
警告 FS0020:此表达式应具有“单元”类型,但具有“登录选项”类型。使用 'ignore' 丢弃表达式的结果,或使用 'let' 将结果绑定到名称。
如何解决问题?它是处理 Ado.Net 的最佳方式吗?
type login = { userName: string; password: string; downloadDir: string}
let getLogin =
let conn = new SqlConnection("Data Source=......;Integrated Security=SSPI;")
try
conn.Open()
let cmd = new SqlCommand("select * FROM Login where website = 'xxx'", conn)
cmd.CommandType <- CommandType.Text
use reader = cmd.ExecuteReader()
if (reader.HasRows) then (
reader.Read() |> ignore
let userName = reader.GetString(5)
let password = reader.GetString(6)
printfn "%s" userName
Some { userName = userName; password = password; downloadDir = "zzz" }
)
else (
printfn "Cannot find login information."
None
)
conn.Close()
with
| :? System.Data.SqlClient.SqlException as sqlEx -> printfn "%A" sqlEx.Message
| _ -> printfn "Unknown Exception"
更新: 我已根据 Thomas 的建议更新了代码。
let getLogin =
use conn = new SqlConnection("Data Source=mdyptcafgprd101;Initial Catalog=App;Integrated Security=SSPI;")
try
conn.Open()
let cmd = new SqlCommand("select * FROM app.WebCrawler.LoginLbl where website = 'CTSLink'", conn)
cmd.CommandType <- CommandType.Text
use reader = cmd.ExecuteReader()
if (reader.HasRows) then (
reader.Read() |> ignore
let userName = reader.GetString(5)
let password = reader.GetString(6)
Some { userName = userName; password = password; downloadDir = "zzz" }
)
else (
printfn "Cannot find login information."
None
)
with
| :? System.Data.SqlClient.SqlException as sqlEx -> printfn "%A" sqlEx.Message
| _ -> printfn "Unknown Exception"
但是,现在它在| :? System... 行出现以下错误。
【问题讨论】: