So far I have found Expert F#: Understanding ADO.NET, combined with some heavy cross-referencing of the MSDN to be the most useful. You usually end up porting the C# samples to F#.

A lot of articles from 2007 and early 2008 make reference to LinqToSQL, but LinqToSql is dead as it is basically making way for the more heavy-weight Entity Framework. So for the time being you are safer with plain ADO.NET.

Because of this I cobbled together a version of the "Expert F#" FSharpWeb.DataSource (which originally used a SQLMetal generated LinqToSql Object Relational Mapper) that is entirely ADO.NET based. I'll include it at the end of this post if you want to use it as a starting point.

I run this code against SQLExpress 2008 that has the Northwind database loaded.
You can get the Northwind and Pubs loading scripts from:
Northwind and pubs Sample Databases for SQL Server 2000.

The databases can then be loaded from the command line with:

1
2
sqlcmd -E -S computer-name\SQLEXPRESS -i instnwnd.sql 
sqlcmd -E -S computer-name\SQLEXPRESS -i instpubs.sql

where you replace "computer-name" with your "Computer Name"

Good Luck!

PS: Expert F# is due to be replaced with The Definitive Guide to F# in late 2011 or early 2010.

DataSource.fs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#light
// DataSource.fs
namespace FSharpWeb

open System
open System.Data
open System.Data.Common
open System.Data.SqlClient

type CategoryInfo = { CategoryID: int; Name: string }
type ProductInfo = {ProductName: string; Price: System.Decimal; }

module DataSource =
    let private GetConnStr = "Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
        
    let private GetItems connStr cmdText parameterInfo makeItemInfo =
        let selectParameterValue (nullable: 'a Nullable) =
            match nullable.HasValue with
            | true  -> (nullable.Value :> obj)
            | false -> (DBNull.Value   :> obj)
            
        let AddQueryParameter (cmd:DbCommand) =
            function | Some(name,value) -> cmd.Parameters.Add(SqlParameter( name, (selectParameterValue value)) ) |> ignore
                     | None             -> ()
            
        seq {
            use conn = new SqlConnection(connStr)
            do conn.Open()
            use cmd = new SqlCommand( cmdText ,conn )
            do  AddQueryParameter cmd parameterInfo
            use reader = cmd.ExecuteReader()
            while reader.Read() do
                yield( makeItemInfo reader )}

    
    let GetCategories () : CategoryInfo seq =
        let makeCategoryInfo (reader:IDataReader) =
            { CategoryID = reader.GetInt32 0 
              Name       = reader.GetString 1 }
        let cmdText = "SELECT [CategoryID], [CategoryName] FROM [Categories]"
        GetItems GetConnStr cmdText None makeCategoryInfo


    let GetProducts (categoryID:int Nullable) : ProductInfo seq =
        let makeProductInfo (reader:IDataReader) =
            { ProductName = reader.GetString 0 
              Price       = (if not (reader.IsDBNull 1) then reader.GetDecimal 1  else -1M) }
        let cmdText = "SELECT [ProductName], [UnitPrice] FROM [Products] WHERE [CategoryID] = @CategoryID"
         GetItems GetConnStr cmdText (Some ("@CategoryID",categoryID)) makeProductInfo

Program.fs

1
2
3
4
5
6
7
8
9
10
11
12
#light
// Program.fs
open System
open FSharpWeb

do
    for c in FSharpWeb.DataSource.GetCategories() do
        Console.WriteLine( "ID: {0} Name: {1}", c.CategoryID, c.Name )
        
    let categoryID = new int Nullable(2)
    for p in FSharpWeb.DataSource.GetProducts(categoryID) do
        Console.WriteLine( "Name: {0} Price: {1}", p.ProductName, p.Price )
By on 7/24/2009 3:05 PM ()

I would like to add my voice to the request as well.

I am reading both "F# for Scientists" and "Foundations of F#," but the database portions of those books don't work out the way things are written. Connection strings don't work, there is no config file created by default--lots of problems.

A simple tutorial somewhere would be useful.

By on 7/24/2009 1:01 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper