diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.DBNull.Class/fs/DBNullExamples.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.DBNull.Class/fs/DBNullExamples.fs new file mode 100644 index 00000000000..ac4beaaa597 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.DBNull.Class/fs/DBNullExamples.fs @@ -0,0 +1,62 @@ +open System +open System.Data +open System.Data.OleDb + +type DBNullExample() = + // + 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 + // + +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"] \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.DBNull.Class/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.DBNull.Class/fs/fs.fsproj new file mode 100644 index 00000000000..8284a679eb8 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.DBNull.Class/fs/fs.fsproj @@ -0,0 +1,12 @@ + + + Exe + net6.0 + + + + + + + + \ No newline at end of file diff --git a/xml/System/DBNull.xml b/xml/System/DBNull.xml index 08eafb2d22f..316525fca26 100644 --- a/xml/System/DBNull.xml +++ b/xml/System/DBNull.xml @@ -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"::: ]]> @@ -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"::: ]]>