我一直在試圖找出從F#插入SQL Server的方法。F#向SQL Server插入一個列表
我有一個F#函數,可以在用戶定義的模式之後遍歷文件夾內的所有文件。然後我可以使用返回的數據放入列表或(理想情況下)插入到數據庫中。
我已經有一個工作嵌入到-SQL函數可以正常工作:
let execNonQuery conn s =
let comm =
new SqlCeCommand(s, conn)
try
comm.ExecuteNonQuery() |> ignore
with e ->
printf "Error : %A\n" e
let string = "insert into MyTable (MyColumn) values ('test .. again')"
execNonQuery conn string; // works
我試圖讓這個方法才能正常工作:
let rec getAllFiles dir pattern =
seq { yield! Directory.EnumerateFiles(dir, pattern)
for d in Directory.EnumerateDirectories(dir) do
yield! getAllFiles d pattern }
let getApplications (dir : string) (extension : string) =
getAllFiles dir extension
//|> Seq.toList // If I need to create a list of returned values
|> Seq.iter (fun s -> SQLInsertString s) // This does not work as it complains about the function not being of type unit
如果我使用Seq.toList
只能按以下方式調用函數,它的工作原理:
getApplications "C:\Admin" "*.txt" // works
另一件我不明白的事情是,如何創建一個接受Value的字符串的工作插入命令。例如:
let SQLInsertString s = "insert into MyTable (MyColumn) values (%s)" //does not work
非常感謝Tomas,作品像一個魅力。現在我也看到了它更好 – 2012-02-15 15:35:45