Skip to content

Commit 3f6237c

Browse files
authored
DBNull F# snippet (#7598)
1 parent 30be977 commit 3f6237c

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
open System
2+
open System.Data
3+
open System.Data.OleDb
4+
5+
type DBNullExample() =
6+
// <Snippet1>
7+
member this.OutputLabels(dt: DataTable) =
8+
let mutable label = ""
9+
10+
// Iterate rows of table
11+
for row in dt.Rows do
12+
let mutable label = String.Empty
13+
label <- label + this.AddFieldValue(label, row, "Title")
14+
label <- label + this.AddFieldValue(label, row, "FirstName")
15+
label <- label + this.AddFieldValue(label, row, "MiddleInitial")
16+
label <- label + this.AddFieldValue(label, row, "LastName")
17+
label <- label + this.AddFieldValue(label, row, "Suffix")
18+
label <- label + "\n"
19+
label <- label + this.AddFieldValue(label, row, "Address1")
20+
label <- label + this.AddFieldValue(label, row, "AptNo")
21+
label <- label + "\n"
22+
let labelLen = label.Length
23+
label <- label + this.AddFieldValue(label, row, "Address2")
24+
let labelLen =
25+
if label.Length <> labelLen then
26+
label + "\n"
27+
else label
28+
label <- label + this.AddFieldValue(label, row, "City")
29+
label <- label + this.AddFieldValue(label, row, "State")
30+
label <- label + this.AddFieldValue(label, row, "Zip")
31+
printfn $"{label}"
32+
printfn ""
33+
34+
member _.AddFieldValue(label: string, row: DataRow, fieldName: string) =
35+
if DBNull.Value.Equals row[fieldName] |> not then
36+
(string row[fieldName]) + " "
37+
else
38+
String.Empty
39+
// </Snippet1>
40+
41+
let ex = DBNullExample()
42+
let conn = new OleDbConnection()
43+
let cmd = new OleDbCommand()
44+
let adapter = new OleDbDataAdapter()
45+
let ds = new DataSet()
46+
let dbFilename = @"c:\Data\contacts.mdb"
47+
48+
// Open database connection
49+
conn.ConnectionString <- "Provider=Microsoft.Jet.OLEDB.4.0Data Source=" + dbFilename + ""
50+
conn.Open()
51+
// Define command : retrieve all records in contact table
52+
cmd.CommandText <- "SELECT * FROM Contact"
53+
cmd.Connection <- conn
54+
adapter.SelectCommand <- cmd
55+
// Fill dataset
56+
ds.Clear()
57+
adapter.Fill(ds, "Contact")
58+
|> ignore
59+
// Close connection
60+
conn.Close()
61+
// Output labels to console
62+
ex.OutputLabels ds.Tables["Contact"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="System.Data.OleDb" Version="6.0.0" />
8+
</ItemGroup>
9+
<ItemGroup>
10+
<Compile Include="DBNullExamples.fs" />
11+
</ItemGroup>
12+
</Project>

xml/System/DBNull.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
The following example calls the `DBNull.Value.Equals` method to determine whether a database field in a contacts database has a valid value. If it does, the field value is appended to the string output in a label.
8686
8787
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.DBNull.Class/cs/DBNullExamples.cs" id="Snippet1":::
88+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.DBNull.Class/fs/DBNullExamples.fs" id="Snippet1":::
8889
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.DBNull.Class/vb/DBNullExamples.vb" id="Snippet1":::
8990
9091
]]></format>
@@ -1133,6 +1134,7 @@
11331134
The following example calls the `DBNull.Value.Equals` method to determine whether a database field in a contacts database has a valid value. If it does, the field value is appended to the string output in a label.
11341135
11351136
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.DBNull.Class/cs/DBNullExamples.cs" id="Snippet1":::
1137+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.DBNull.Class/fs/DBNullExamples.fs" id="Snippet1":::
11361138
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.DBNull.Class/vb/DBNullExamples.vb" id="Snippet1":::
11371139
11381140
]]></format>

0 commit comments

Comments
 (0)