1
1
// Learn more about F# at http://fsharp.org
2
2
3
- open FSharpRepoDB
3
+ open System
4
4
open Microsoft.Data .SqlClient
5
5
open RepoDb
6
6
open RepoDb.Extensions
7
- open System
7
+ open FSharpRepoDB.Types
8
+
9
+ let url ( argv : string array ) =
10
+ let getFromEnv =
11
+ System.Environment.GetEnvironmentVariable( " MSSQL_DB_URL" )
12
+ |> Option.ofObj
13
+
14
+ let fromArgsOrEnv =
15
+ argv
16
+ |> Array.tryFind( fun arg -> arg.Contains " --dburl=" )
17
+ |> Option.map ( fun f -> f.Split( '=' ) |> Seq.tryLast)
18
+ |> Option.flatten
19
+ |> Option.orElse getFromEnv
20
+
21
+ fromArgsOrEnv |> Option.defaultValue " Server=127.0.0.1;Database=TestDB;User Id=sa;Password=Password1!;"
8
22
9
23
[<EntryPoint>]
10
24
let main argv =
@@ -13,20 +27,21 @@ let main argv =
13
27
SqlServerBootstrap.Initialize()
14
28
15
29
// Open the connection
16
- let connection = ( new SqlConnection( " Server=.;Database=TestDB;Integrated Security=SSPI; " )) .EnsureOpen()
30
+ let connection = ( new SqlConnection( url argv )) .EnsureOpen()
17
31
18
32
// Get the fields
19
33
let personType = typedefof< Person>
20
34
let dbFields = DbFieldCache.Get( connection, " Person" , null ) .AsList()
21
35
let fields = FieldCache.Get( personType) .AsList()
22
36
23
- // Create the type
24
- let person = new Person ( Convert.ToInt64( 0 ), " John Doe" , 32 , " New York" , true )
37
+ let person = { Id = 0 L; Name = " John Doe" ; Address = " New York" ; Age = 32 ; IsActive = true }
25
38
26
39
// Insert (Generic-Based)
27
40
let id = connection.Insert< Person, int64>( person)
28
41
Console.WriteLine( Convert.ToString( id))
29
42
43
+ // Create PersonLike Record the type
44
+ let person = {| Name = " John Doe" ; Age = 32 ; Address = " New York" ; IsActive = true |}
30
45
// Insert (Table-Based)
31
46
let id = connection.Insert< int64>( ClassMappedNameCache.Get< Person>(), person)
32
47
Console.WriteLine( Convert.ToString( id))
0 commit comments