Table of Contents

  • Overview
    • Prerequisite
  • Installing NuGet
  • Creating New Project
  • Installing Soma
  • Creating Sample Table and Data
  • Creating Program
  • Executing Program

Overview

This document describes how to create simple F# program from scratch.

Prerequisite

Following software must have been installed in your computer.

  • Visual Studio 2010 Professional
  • SQL Server 2008 Express

Installing NuGet

Install Visual Studio add-in of NuGet. 

Creating New Project

From Visual Studio menu, Open the "New Project" dialog and create a F# console project. 

new_project.png

Inatalling Soma

Select the "Manage NuGet Packages..." item from the menu.

manage_nuget_packages.png

Enter the string "Soma" into the upper right text box.

install.png

Click the "Install" button of the Soma package. When the License Acceptance dialog is open, click "I Accept". 

license_acceptance.png

After installation, the project structure is as follows.

project_after_installation.bmp

Creating Sample Table and Data

From Visual Studio menu, connect to SQL Server.

new_query_connection.bmp

Execute following SQL.

use tempdb;

create table Employee (
  EmployeeId integer not null primary key, 
  EmployeeName varchar(20), 
  Hiredate date,
  Salary numeric(7,2),
  VersionNo integer
);

insert into Employee values(1, 'SMITH', '1980-12-17', 800, 1);
insert into Employee values(2, 'ALLEN', '1981-02-20', 1600, 1);
insert into Employee values(3, 'WARD', '1981-02-22', 1250, 1);

Creating Program

Open the Program.fs file and write following code.

open System
open Soma.Core

let config = 
  { new MsSqlConfig() with
    member this.ConnectionString = "Data Source=.;Initial Catalog=tempdb;Integrated Security=True" }

type Employee = 
  { [<Id>]
    EmployeeId : int 
    EmployeeName : string
    Hiredate : DateTime
    Salary : decimal
    [<Version>]
    VersionNo : int }

[<EntryPoint>]
let main args =
  let employee = Db.find<Employee> config [1]
  printfn "before update: %A" employee 
  let employee = Db.update<Employee> config { employee with Salary = employee.Salary * 2M }
  printfn "after update: %A" employee 
  Console.ReadKey() |> ignore
  0

Build the project.

Executing Program

On Visual Studio, press F5 key and execute the program. The console is shown.

result.png

Last edited Aug 21, 2011 at 12:35 AM by toshihiro, version 4

Comments

No comments yet.