Channels ▼


Information-Rich Programming With F# 3.0

The following code shows a simple example that uses the SqlDataConnection type provider to retrieve a player and display its name. Of course, you will have to make the necessary changes to the ConnectionString in order to establish a connection with your database.

module PlayersAnalysis = 
    open System
    open Microsoft.FSharp.Data.TypeProviders

    type SqlServerConnection = SqlDataConnection<ConnectionString = 
        @"Data Source=WIN8X64;Initial Catalog=Retrogames;Integrated Security=True">

    let db = SqlServerConnection.GetDataContext()

    let GetPlayerById(id) = 
        query {
            for player in db.Player do
            where (player.PlayerId = id)
            select player
    let ReadAnyKey() = Console.ReadKey() |> ignore
    let onePlayer = GetPlayerById 5

        |> printfn "The player's name is %s"

If you've worked with previous F# versions, you will notice that I didn't mention the need of an F# PowerPack when working with query expressions. F# 3.0 doesn't require F# PowerPack — query expressions provide support for LINQ in F# without requiring you to download additional libraries. All you have to do is include the appropriate references.

The previous code snippet is easy to understand. SqlServerConnection is the type abbreviation (also known as alias) for the SqlDataConnection type provider with the specified connection string. The GetDataContext static method gets a simplified data context for the SQL connection. db is of type SqlServerConnection.ServiceTypes.SimpleDataContextTypes.Retrogames. If you enter the following lines, you will notice how Intellisense makes it easy to generate the query when you enter db.P because you have two valid options: Player and PlayerScore (see Figure 10).

let GetPlayerById(id) = 
    query {
        for player in db.P

Figure 10: Intellisense makes it easy to know the entity name.

As you continue building your query expression, Intellisense provides you with additional information. For example, after you write player. in a where statement, Intellisene displays the different fields available for the Player entity (see Figure 11).

query {
    for player in db.Player do
    where (player.

Figure 11: Intellisense makes it easy to know the field name.

Once you created the GetPlayerById function and you write the line that defines onePlayer, the type inference mechanism knows that onePlayer is of type SqlServerConnection.ServiceTypes.Player:

let onePlayer = GetPlayerById 5

Thus, after you write onePlayer, Intellisene will display the different fields available for the Player entity (see Figure 12). This way, you focus on the code and you can consume data in the same way you are accustomed to in Microsoft SQL Server Management Studio, but focusing primarily on creating your F# algorithms.

Figure 12: F# type inference + Intellisense provide you with the field names for onePlayer.

In this example, I specified the connection string in an unsecure way and without additional parameters. If you're building a library or an application, you need to make sure you build the connection string in a different way. However, I'm just providing an example of how to create quick prototypes for your algorithms with live connection to the data source. In addition, you will need to consider other parameters for the connection string, such as the timeout and the parameters that control the database elements that you want to be exposed as types.

In this first article, I've provided a brief overview of type providers and one of the options to establish a quick connection to a SQL database and start creating F# algorithms for this kind of data source. Now, you know all the necessary steps to start using the different type providers for SQL data sources. For example, if you are already using Entity Framework, you can select the appropriate type provider (either EdmxFile or SqlEntityConnection) and take advantage of all the mappings effort done through the ORM.

Obviously, the advantages of type providers are even more important when the data sources are unknown and more complex. In the More Information-Rich Programming with F#

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.