For f# to talk to a database, I presume you turn to some code that looks quite a lot like C# code, using some NET libraries (ado.net for example) and quite a lot of imperative code that has, by definition, quite a lot of side-effects..
对于f#与数据库交谈,我假设您转向一些看起来非常像C#代码的代码,使用一些.NET库(例如ado.net)和相当多的命令式代码,根据定义,它们非常类似很多副作用..
Or am I missing something here? Has F# some beauty to offer in this domain also?
或者我在这里遗漏了什么? F#在这个领域还有一些美女吗?
And would someone be so kind a to provide me with an example for both reading from an writing to a database?
有人会如此善良地为我提供一个从写入数据库阅读的例子吗?
2 个解决方案
#1
2
You may want to use the accepted answer in this question as a good starting point.
您可能希望在此问题中使用已接受的答案作为一个很好的起点。
F# Beginner: retrieving an array of data from a server
F#Beginner:从服务器检索数据数组
Depending on the database you are using you may get some other choices, but start with something fairly functional and you can improve on it as you gain experience.
根据您使用的数据库,您可能会得到一些其他选择,但从相当实用的功能开始,您可以在获得经验时对其进行改进。
#2
4
When I needed to do some database access from F# in a test project, I ended up using LINQ to SQL from F#. I just added a C# project to the solution, put the DataContext in the C# project, and used the generated C# LINQ to SQL classes in my F# project.
当我需要在测试项目中从F#进行一些数据库访问时,我最终使用了来自F#的LINQ to SQL。我刚刚在解决方案中添加了一个C#项目,将DataContext放在C#项目中,并在我的F#项目中使用生成的C#LINQ to SQL类。
First you need to reference the assemblies FSharp.PowerPack and FSharp.PowerPack.Linq. Then you can open Microsoft.FSharp.Linq
.
首先,您需要引用程序集FSharp.PowerPack和FSharp.PowerPack.Linq。然后,您可以打开Microsoft.FSharp.Linq。
Here's an example that parses "Site" tags out of an XDocument, creating instances of the Site
class (a C# generated LINQ to SQL class), then inserting them into the database using the L2S data context.
这是一个从XDocument中解析“Site”标签,创建Site类实例(C#生成LINQ to SQL类),然后使用L2S数据上下文将它们插入数据库的示例。
let sites = doc.Descendants(ns + "Site")
|> Seq.map (fun el -> new Site (
Url = xstr(el.Element(ns + "DataUrl")),
Rank = xint(el.Element(ns + "Rank"))
))
use db = new SomeDataContext()
db.Sites.InsertAllOnSubmit(sites)
db.SubmitChanges()
As you can see, even though it's using C# classes, it's not entirely imperative code.
正如您所看到的,即使它使用的是C#类,也不是完全必要的代码。
Here's an example of using the F# version of LINQ to find the maximum rank of all the site entries in the database. Yes, this does get translated to SQL and executed in the database.
下面是使用F#版LINQ查找数据库中所有站点条目的最大排名的示例。是的,这确实被转换为SQL并在数据库中执行。
use db = new SomeDataContext()
Query.query <@ seq { for s in db.Sites -> s.Rank } |> Seq.max @>
Finally, here's some more information on LINQ with F#.
最后,这里有关于使用F#的LINQ的更多信息。
#1
2
You may want to use the accepted answer in this question as a good starting point.
您可能希望在此问题中使用已接受的答案作为一个很好的起点。
F# Beginner: retrieving an array of data from a server
F#Beginner:从服务器检索数据数组
Depending on the database you are using you may get some other choices, but start with something fairly functional and you can improve on it as you gain experience.
根据您使用的数据库,您可能会得到一些其他选择,但从相当实用的功能开始,您可以在获得经验时对其进行改进。
#2
4
When I needed to do some database access from F# in a test project, I ended up using LINQ to SQL from F#. I just added a C# project to the solution, put the DataContext in the C# project, and used the generated C# LINQ to SQL classes in my F# project.
当我需要在测试项目中从F#进行一些数据库访问时,我最终使用了来自F#的LINQ to SQL。我刚刚在解决方案中添加了一个C#项目,将DataContext放在C#项目中,并在我的F#项目中使用生成的C#LINQ to SQL类。
First you need to reference the assemblies FSharp.PowerPack and FSharp.PowerPack.Linq. Then you can open Microsoft.FSharp.Linq
.
首先,您需要引用程序集FSharp.PowerPack和FSharp.PowerPack.Linq。然后,您可以打开Microsoft.FSharp.Linq。
Here's an example that parses "Site" tags out of an XDocument, creating instances of the Site
class (a C# generated LINQ to SQL class), then inserting them into the database using the L2S data context.
这是一个从XDocument中解析“Site”标签,创建Site类实例(C#生成LINQ to SQL类),然后使用L2S数据上下文将它们插入数据库的示例。
let sites = doc.Descendants(ns + "Site")
|> Seq.map (fun el -> new Site (
Url = xstr(el.Element(ns + "DataUrl")),
Rank = xint(el.Element(ns + "Rank"))
))
use db = new SomeDataContext()
db.Sites.InsertAllOnSubmit(sites)
db.SubmitChanges()
As you can see, even though it's using C# classes, it's not entirely imperative code.
正如您所看到的,即使它使用的是C#类,也不是完全必要的代码。
Here's an example of using the F# version of LINQ to find the maximum rank of all the site entries in the database. Yes, this does get translated to SQL and executed in the database.
下面是使用F#版LINQ查找数据库中所有站点条目的最大排名的示例。是的,这确实被转换为SQL并在数据库中执行。
use db = new SomeDataContext()
Query.query <@ seq { for s in db.Sites -> s.Rank } |> Seq.max @>
Finally, here's some more information on LINQ with F#.
最后,这里有关于使用F#的LINQ的更多信息。