-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathProgram.fs
More file actions
96 lines (81 loc) · 3.63 KB
/
Program.fs
File metadata and controls
96 lines (81 loc) · 3.63 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Learn more about F# at http://fsharp.org
open System
open System.Data.SqlClient
[<EntryPoint>]
let main argv =
printfn "Connect to SQL Server and demo Create, Read, Update and Delete operations."
let builder = new SqlConnectionStringBuilder()
builder.DataSource <- "localhost"
builder.UserID <- "sa"
builder.Password <- "your_password"
builder.InitialCatalog <- "master"
printf "Connecting to SQL Server ... "
use connection = new SqlConnection(builder.ConnectionString)
try
connection.Open()
printfn "Done."
// Create a sample database
printf "Dropping and creating database 'FSharpSampleDB' ... "
let sql = "DROP DATABASE IF EXISTS [FSharpSampleDB]; CREATE DATABASE [FSharpSampleDB]"
use command = new SqlCommand(sql, connection)
command.ExecuteNonQuery() |> ignore
printfn "Done."
// Create a Table and insert some sample data
printf "Creating sample table with data, press any key to continue..."
Console.ReadKey(true) |> ignore
let sql = "
USE FSharpSampleDB;
CREATE TABLE Employees (
Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Name NVARCHAR(50),
Location NVARCHAR(50)
);
INSERT INTO Employees (Name, Location) VALUES
(N'Tom', N'United States'),
(N'Krzysztof', N'Poland'),
(N'Isaac', N'Germany'); "
use command = new SqlCommand(sql, connection)
command.ExecuteNonQuery() |> ignore
printfn "Done."
// INSERT demo
printf "Inserting a new row into table, press any key to continue... "
Console.ReadKey(true) |> ignore
let sql = "
INSERT Employees (Name, Location)
VALUES (@name, @location);"
use command = new SqlCommand(sql, connection)
command.Parameters.AddWithValue("@name", "Don") |> ignore
command.Parameters.AddWithValue("@location", "United Kingdom") |> ignore
let rowsAffected = command.ExecuteNonQuery()
printfn "%i row(s) inserted" rowsAffected
// UPDATE demo
let userToUpdate = "Tom";
printf "Updating 'Location' for user '%s', press any key to continue... " userToUpdate
Console.ReadKey(true) |> ignore
let sql = "UPDATE Employees SET Location = N'United Kingdom' WHERE Name = @name"
use command = new SqlCommand(sql, connection)
command.Parameters.AddWithValue("@name", userToUpdate) |> ignore
let rowsAffected = command.ExecuteNonQuery()
printfn "%i row(s) updated" rowsAffected
// DELETE demo
let userToDelete = "Don";
printf "Deleting user '%s', press any key to continue... " userToDelete
Console.ReadKey(true) |> ignore
let sql = "DELETE FROM Employees WHERE Name = @name;"
use command = new SqlCommand(sql, connection)
command.Parameters.AddWithValue("@name", userToDelete) |> ignore
let rowsAffected = command.ExecuteNonQuery()
printfn "%i row(s) deleted" rowsAffected
// READ demo
printfn "Reading data from table, press any key to continue... "
Console.ReadKey(true) |> ignore
let sql = "SELECT Id, Name, Location FROM Employees;"
use command = new SqlCommand(sql, connection)
use reader = command.ExecuteReader()
while reader.Read() do
printfn "%i %s %s" (reader.GetInt32(0)) (reader.GetString(1)) (reader.GetString(2))
with
| ex -> printfn "%O" ex
printfn "All done. Press the any key to finish..."
Console.ReadKey(true) |> ignore
0 // return an integer exit code