Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.DBNull F# snippet #7598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
open System
open System.Data
open System.Data.OleDb

type DBNullExample() =
// <Snippet1>
member this.OutputLabels(dt: DataTable) =
let mutable label = ""

// Iterate rows of table
for row in dt.Rows do
let mutable label = String.Empty
label <- label + this.AddFieldValue(label, row, "Title")
label <- label + this.AddFieldValue(label, row, "FirstName")
label <- label + this.AddFieldValue(label, row, "MiddleInitial")
label <- label + this.AddFieldValue(label, row, "LastName")
label <- label + this.AddFieldValue(label, row, "Suffix")
label <- label + "\n"
label <- label + this.AddFieldValue(label, row, "Address1")
label <- label + this.AddFieldValue(label, row, "AptNo")
label <- label + "\n"
let labelLen = label.Length
label <- label + this.AddFieldValue(label, row, "Address2")
let labelLen =
if label.Length <> labelLen then
label + "\n"
else label
label <- label + this.AddFieldValue(label, row, "City")
label <- label + this.AddFieldValue(label, row, "State")
label <- label + this.AddFieldValue(label, row, "Zip")
printfn $"{label}"
printfn ""

member _.AddFieldValue(label: string, row: DataRow, fieldName: string) =
if DBNull.Value.Equals row[fieldName] |> not then
(string row[fieldName]) + " "
else
String.Empty
// </Snippet1>

let ex = DBNullExample()
let conn = new OleDbConnection()
let cmd = new OleDbCommand()
let adapter = new OleDbDataAdapter()
let ds = new DataSet()
let dbFilename = @"c:\Data\contacts.mdb"

// Open database connection
conn.ConnectionString <- "Provider=Microsoft.Jet.OLEDB.4.0Data Source=" + dbFilename + ""
conn.Open()
// Define command : retrieve all records in contact table
cmd.CommandText <- "SELECT * FROM Contact"
cmd.Connection <- conn
adapter.SelectCommand <- cmd
// Fill dataset
ds.Clear()
adapter.Fill(ds, "Contact")
|> ignore
// Close connection
conn.Close()
// Output labels to console
ex.OutputLabels ds.Tables["Contact"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.OleDb" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="DBNullExamples.fs" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions xml/System/DBNull.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
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.

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.DBNull.Class/cs/DBNullExamples.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.DBNull.Class/fs/DBNullExamples.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.DBNull.Class/vb/DBNullExamples.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -1133,6 +1134,7 @@
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.

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.DBNull.Class/cs/DBNullExamples.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.DBNull.Class/fs/DBNullExamples.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.DBNull.Class/vb/DBNullExamples.vb" id="Snippet1":::

]]></format>
Expand Down