@@ -46,10 +46,10 @@ CommandText
4646open FSharp.Data
4747
4848[<Literal>]
49- let connectionString = @" Data Source=.;Initial Catalog=AdventureWorks2014 ;Integrated Security=True"
49+ let connStr = @" Data Source=.;Initial Catalog=AdventureWorks2012 ;Integrated Security=True"
5050
5151//Inline T-SQL text convinient for short queries
52- type GetDate = SqlCommandProvider< " SELECT GETDATE() AS Now" , connectionString >
52+ type GetDate = SqlCommandProvider< " SELECT GETDATE() AS Now" , connStr >
5353
5454//More complex queries are better off extracted to stand-alone literals
5555
@@ -71,10 +71,12 @@ let fibonacci = "
7171 FROM Fibonacci
7272"
7373
74- type FibonacciQuery = SqlCommandProvider< fibonacci, connectionString >
74+ type FibonacciQuery = SqlCommandProvider< fibonacci, connStr >
7575
76- ( new FibonacciQuery())
77- .Execute( 10 L)
76+ do
77+ let cmd = new FibonacciQuery( connStr)
78+
79+ cmd.Execute( 10 L)
7880 |> Seq.map Option.get
7981 |> Seq.toArray
8082 |> printfn " First 10 fibonacci numbers: %A "
@@ -103,7 +105,7 @@ For example, it can be handed over to DBA team for optimization. It's harder to
103105are mixed together (LINQ).
104106*)
105107
106- let cmd = new SqlCommandProvider< " GetDate.sql" , connectionString >( )
108+ let cmd = new SqlCommandProvider< " GetDate.sql" , connStr >( connStr )
107109cmd.Execute() |> ignore
108110
109111(**
@@ -118,7 +120,7 @@ Below is an example of SQL Table-Valued Function usage.
118120*)
119121
120122type GetContactInformation =
121- SqlCommandProvider< " SELECT * FROM dbo.ufnGetContactInformation(@PersonId)" , connectionString >
123+ SqlCommandProvider< " SELECT * FROM dbo.ufnGetContactInformation(@PersonId)" , connStr >
122124
123125(**
124126### Syntax errors
@@ -147,9 +149,9 @@ type FizzOrBuzz = SqlCommandProvider<"
147149 WHEN @x % 3 = 0 THEN 'Fizz'
148150 WHEN @x % 5 = 0 THEN 'Buzz'
149151 ELSE CONCAT(@x, '') --use concat to avoid nullable column
150- END" , connectionString >
152+ END" , connStr >
151153
152- let fizzOrBuzz = new FizzOrBuzz()
154+ let fizzOrBuzz = new FizzOrBuzz( connStr )
153155printfn " Answer on interview:\n %A " [ for i = 1 to 100 do yield ! fizzOrBuzz.Execute( i) ]
154156
155157(**
@@ -164,7 +166,7 @@ Connection string can be provided either via literal (all examples above) or inl
164166
165167//Inline
166168type Get42 =
167- SqlCommandProvider< " SELECT 42" , @" Data Source=.;Initial Catalog=AdventureWorks2014 ;Integrated Security=True" >
169+ SqlCommandProvider< " SELECT 42" , @" Data Source=.;Initial Catalog=AdventureWorks2012 ;Integrated Security=True" >
168170
169171(**
170172
@@ -176,10 +178,10 @@ The other option is to supply connection string name from config file.
176178*)
177179
178180//default config file name is app.config or web.config
179- type Get43 = SqlCommandProvider< " SELECT 43" , " name=AdventureWorks2014 " >
181+ type Get43 = SqlCommandProvider< " SELECT 43" , " name=AdventureWorks " >
180182
181183//specify ANY other file name (including web.config) explicitly
182- type Get44 = SqlCommandProvider< " SELECT 44" , " name=AdventureWorks2014 " , ConfigFile = " user.config" >
184+ type Get44 = SqlCommandProvider< " SELECT 44" , " name=AdventureWorks " , ConfigFile = " user.config" >
183185
184186(**
185187I would like to emphasize that `ConfigFile` is about ***design time only*.
@@ -223,7 +225,7 @@ let get42 = new Get42(runTimeConnStr)
223225//Factory or IOC of choice to avoid logic duplication. Use F# ctor static constraints.
224226module DB =
225227 [<Literal>]
226- let connStr = @" Data Source=.;Initial Catalog=AdventureWorks2014 ;Integrated Security=True"
228+ let connStr = @" Data Source=.;Initial Catalog=AdventureWorks2012 ;Integrated Security=True"
227229
228230 open System.Data .SqlClient
229231
@@ -245,7 +247,7 @@ let dbCmd2: DB.MyCmd2 = DB.createCommand()
245247//Static type property ConnectionStringOrName that has exactly same value as passed into SqlCommandProvider helps.
246248module DataAccess =
247249 [<Literal>]
248- let adventureWorks = @" Data Source=.;Initial Catalog=AdventureWorks2014 ;Integrated Security=True"
250+ let adventureWorks = @" Data Source=.;Initial Catalog=AdventureWorks2012 ;Integrated Security=True"
249251 [<Literal>]
250252 let master = @" Data Source=.;Initial Catalog=master;Integrated Security=True"
251253
@@ -279,32 +281,34 @@ let bitCoinName = "Bitcoin"
279281
280282type DeleteBitCoin =
281283 SqlCommandProvider< " DELETE FROM Sales.Currency WHERE CurrencyCode = @Code"
282- , connectionString >
284+ , connStr >
283285type InsertBitCoin =
284286 SqlCommandProvider< " INSERT INTO Sales.Currency VALUES(@Code, @Name, GETDATE())"
285- , connectionString >
287+ , connStr >
286288type GetBitCoin =
287289 SqlCommandProvider< " SELECT CurrencyCode, Name FROM Sales.Currency WHERE CurrencyCode = @code"
288- , connectionString >
290+ , connStr >
289291
290292do
291- let cmd = new DeleteBitCoin() in cmd.Execute( bitCoinCode) |> ignore
292- let conn = new System.Data.SqlClient.SqlConnection( connectionString )
293+ let cmd = new DeleteBitCoin( connStr ) in cmd.Execute( bitCoinCode) |> ignore
294+ let conn = new System.Data.SqlClient.SqlConnection( connStr )
293295 conn.Open()
294296 let tran = conn.BeginTransaction()
295297
296- let cmd = new SqlCommandProvider< " INSERT INTO Sales.Currency VALUES(@Code, @Name, GETDATE())"
297- , connectionString>()
298+ let cmd =
299+ new SqlCommandProvider< "
300+ INSERT INTO Sales.Currency VALUES(@Code, @Name, GETDATE())
301+ " , connStr>( connStr)
298302
299- use insert = InsertBitCoin. Create( transaction = tran)
303+ use insert = InsertBitCoin.Create( conn , transaction = tran)
300304 assert ( insert.Execute( bitCoinCode, bitCoinName) = 1 )
301305
302- use get = new GetBitCoin( transaction = tran)
306+ use get = new GetBitCoin( conn , transaction = tran)
303307 assert ( get.Execute( bitCoinCode) |> Seq.length = 1 )
304308
305309 tran.Rollback()
306310
307- assert ( GetBitCoin.Create() .Execute( bitCoinCode) |> Seq.length = 0 )
311+ assert ( GetBitCoin.Create( connStr ) .Execute( bitCoinCode) |> Seq.length = 0 )
308312
309313(**
310314
@@ -316,7 +320,7 @@ to create command instances.
316320
317321open System
318322
319- type AdventureWorks2012 = SqlProgrammabilityProvider< connectionString >
323+ type AdventureWorks2012 = SqlProgrammabilityProvider< connStr >
320324
321325(**
322326
@@ -328,10 +332,10 @@ But there are rare cases when you prefer to handle NULL input values inside T-SQ
328332*)
329333
330334type IncrBy = SqlCommandProvider< " SELECT @x + ISNULL(CAST(@y AS INT), 1) " ,
331- connectionString ,
335+ connStr ,
332336 AllParametersOptional = true ,
333337 SingleRow = true >
334- let incrBy = new IncrBy()
338+ let incrBy = new IncrBy( connStr )
335339//pass both params passed
336340incrBy.Execute( Some 10 , Some 2 ) = Some( Some 12 ) //true
337341//omit second parameter. default to 1
361365</pre>
362366*)
363367
364- type TableValuedSample = SqlCommandProvider< " exec myProc @x" , connectionString >
368+ type TableValuedSample = SqlCommandProvider< " exec myProc @x" , connStr >
365369type TVP = TableValuedSample.MyTableType
366- let tvpSp = new TableValuedSample()
370+ let tvpSp = new TableValuedSample( connStr )
367371//nullable columns mapped to optional ctor params
368372tvpSp.Execute( x = [ TVP( myId = 1 , myName = Some " monkey" ); TVP( myId = 2 ) ])
369373
@@ -374,7 +378,7 @@ Same with `SqlProgrammabilityProvider<...>`
374378type T = AdventureWorks2012.dbo.`` User-Defined Table Types `` .MyTableType
375379
376380do
377- use cmd = new AdventureWorks2012.dbo.MyProc()
381+ use cmd = new AdventureWorks2012.dbo.MyProc( connStr )
378382 cmd.Execute([ T( myId = 2 ); T( myId = 1 ) ]) |> printfn " %A "
379383
380384(**
@@ -386,7 +390,7 @@ There is additional ExecuteSingle/ AsyncExecuteSingle to opt-in for singleton re
386390*)
387391
388392do
389- use cmd = new AdventureWorks2012.dbo.uspGetWhereUsedProductID()
393+ use cmd = new AdventureWorks2012.dbo.uspGetWhereUsedProductID( connStr )
390394
391395 //sync
392396 cmd.Execute( StartProductID = 1 , CheckDate = DateTime( 2013 , 1 , 1 )) |> printfn " %A "
404408*)
405409
406410do
407- use cmd = new AdventureWorks2012.dbo.uspLogError()
411+ use cmd = new AdventureWorks2012.dbo.uspLogError( connStr )
408412 let errorLogId = ref - 1
409413 let recordsAffected = cmd.Execute( errorLogId)
410414 printfn " errorLogId: %i " ! errorLogId
411415
412416do //tupled invocation syntax
413417 //works only in VS 2015 or later because of F# compiler bug
414- use cmd = new AdventureWorks2012.dbo.uspLogError()
418+ use cmd = new AdventureWorks2012.dbo.uspLogError( connStr )
415419 let _ , errorLogId = cmd.Execute()
416420 printfn " errorLogId: %i " errorLogId
417421
418422do //mutable bindgings
419- use cmd = new AdventureWorks2012.dbo.uspLogError()
423+ use cmd = new AdventureWorks2012.dbo.uspLogError( connStr )
420424 let mutable errorLogId = - 1
421425 let recordsAffected = cmd.Execute(& errorLogId)
422426 printfn " errorLogId: %i " errorLogId
@@ -428,13 +432,14 @@ RETURN_VALUE will be the last byref parameter.
428432*)
429433
430434do
431- use cmd = new SqlProgrammabilityProvider< connectionString, UseReturnValue = true >. dbo.uspLogError()
435+ use cmd =
436+ new SqlProgrammabilityProvider< connStr, UseReturnValue = true >. dbo.uspLogError( connStr)
432437 let recordsAffected , errorLogId , returnValue = cmd.Execute()
433438 printfn " recordsAffected: %i , errorLogId: %i , returnValue: %i " recordsAffected errorLogId returnValue
434439
435440(**
436441Things get interesting when stored procedure return both set of rows and output parameters.
437- I won t show any sample code because AdventureWorks database doesn t have such procedure.
442+ I won' t show any sample code because AdventureWorks database doesn' t have such procedure.
438443But the only change comparing to non-query stored procedure that instead of returning number
439444of affected records it returns F# list of records.
440445Notice that list is data structure as oppose to lazy evaluated seq<_> computation.
0 commit comments