|
1 | 1 | # SqlClient providers |
2 | 2 |
|
3 | | -SqlCommandProvider provides statically typed access to input parameters and result set of T-SQL command in idiomatic F# way. |
| 3 | +## SqlCommandProvider |
4 | 4 |
|
5 | | -SqlProgrammabilityProvider exposes Stored Procedures, User-Defined Types and User-Defined Functions in F# code. |
| 5 | +Provides statically typed access to input parameters and result set of T-SQL command in idiomatic F# way. |
| 6 | + |
| 7 | +```fsharp |
| 8 | +open FSharp.Data |
| 9 | +
|
| 10 | +[<Literal>] |
| 11 | +let connectionString = "Data Source=.;Initial Catalog=AdventureWorks2012;Integrated Security=True" |
| 12 | +
|
| 13 | +// The query below retrieves top 3 sales representatives from North American region with YTD sales of more than one million. |
| 14 | +
|
| 15 | +do |
| 16 | + use cmd = new SqlCommandProvider<" |
| 17 | + SELECT TOP(@topN) FirstName, LastName, SalesYTD |
| 18 | + FROM Sales.vSalesPerson |
| 19 | + WHERE CountryRegionName = @regionName AND SalesYTD > @salesMoreThan |
| 20 | + ORDER BY SalesYTD |
| 21 | + " , connectionString>(connectionString) |
| 22 | +
|
| 23 | + cmd.Execute(topN = 3L, regionName = "United States", salesMoreThan = 1000000M) |> printfn "%A" |
| 24 | +``` |
| 25 | +output |
| 26 | +```fsharp |
| 27 | +seq |
| 28 | + [("Pamela", "Ansman-Wolfe", 1352577.1325M); |
| 29 | + ("David", "Campbell", 1573012.9383M); |
| 30 | + ("Tete", "Mensa-Annan", 1576562.1966M)] |
| 31 | +``` |
| 32 | + |
| 33 | +## SqlProgrammabilityProvider |
| 34 | + |
| 35 | +Exposes Tables, Stored Procedures, User-Defined Types and User-Defined Functions in F# code. |
| 36 | + |
| 37 | +```fsharp |
| 38 | +type AdventureWorks = SqlProgrammabilityProvider<connectionString> |
| 39 | +do |
| 40 | + use cmd = new AdventureWorks.dbo.uspGetWhereUsedProductID(connectionString) |
| 41 | + for x in cmd.Execute( StartProductID = 1, CheckDate = System.DateTime(2013,1,1)) do |
| 42 | + //check for nulls |
| 43 | + match x.ProductAssemblyID, x.StandardCost, x.TotalQuantity with |
| 44 | + | Some prodAsmId, Some cost, Some qty -> |
| 45 | + printfn "ProductAssemblyID: %i, StandardCost: %M, TotalQuantity: %M" prodAsmId cost qty |
| 46 | + | _ -> () |
| 47 | +``` |
| 48 | +output |
| 49 | +```fsharp |
| 50 | +ProductAssemblyID: 749, StandardCost: 2171.2942, TotalQuantity: 1.00 |
| 51 | +ProductAssemblyID: 750, StandardCost: 2171.2942, TotalQuantity: 1.00 |
| 52 | +ProductAssemblyID: 751, StandardCost: 2171.2942, TotalQuantity: 1.00 |
| 53 | +``` |
| 54 | + |
| 55 | +## SqlEnumProvider |
| 56 | + |
| 57 | +Let's say we need to retrieve number of orders shipped by a certain shipping method since specific date. |
| 58 | + |
| 59 | +```fsharp |
| 60 | +//by convention: first column is Name, second is Value |
| 61 | +type ShipMethod = SqlEnumProvider<" |
| 62 | + SELECT Name, ShipMethodID FROM Purchasing.ShipMethod ORDER BY ShipMethodID", connectionString> |
| 63 | +
|
| 64 | +//Combine with SqlCommandProvider |
| 65 | +do |
| 66 | + use cmd = new SqlCommandProvider<" |
| 67 | + SELECT COUNT(*) |
| 68 | + FROM Purchasing.PurchaseOrderHeader |
| 69 | + WHERE ShipDate > @shippedLaterThan AND ShipMethodID = @shipMethodId |
| 70 | + ", connectionString, SingleRow = true>(connectionString) |
| 71 | + //overnight orders shipped since Jan 1, 2008 |
| 72 | + cmd.Execute( System.DateTime( 2008, 1, 1), ShipMethod.``OVERNIGHT J-FAST``) |> printfn "%A" |
| 73 | +``` |
| 74 | +output |
| 75 | +```fsharp |
| 76 | +Some (Some 1085) |
| 77 | +``` |
| 78 | + |
| 79 | +## SqlFileProvider |
| 80 | + |
| 81 | +```fsharp |
| 82 | +type SampleCommand = SqlFile<"sampleCommand.sql"> |
| 83 | +type SampleCommandRelative = SqlFile<"sampleCommand.sql", "MySqlFolder"> |
| 84 | +
|
| 85 | +use cmd1 = new SqlCommandProvider<SampleCommand.Text, ConnectionStrings.AdventureWorksNamed>() |
| 86 | +use cmd2 = new SqlCommandProvider<SampleCommandRelative.Text, ConnectionStrings.AdventureWorksNamed>() |
| 87 | +``` |
6 | 88 |
|
7 | 89 | More information can be found in the [documentation](http://fsprojects.github.io/FSharp.Data.SqlClient/). |
8 | 90 |
|
|
0 commit comments