@@ -8,31 +8,10 @@ open FSharp.Data.SqlClient
88
99[<Sealed>]
1010[<CompilerMessageAttribute( " This API supports the FSharp.Data.SqlClient infrastructure and is not intended to be used directly from your code." , 101 , IsHidden = true ) >]
11- type DataTable < 'T when 'T :> DataRow > private ( tableName , knownSelectCommand , getDesignTimeConnection : Lazy <_ >) =
12- inherit DataTable( tableName )
11+ type DataTable < 'T when 'T :> DataRow >( selectCommand : SqlCommand , ? connectionString : Lazy <string >) =
12+ inherit DataTable()
1313
1414 let rows = base .Rows
15- let getSelectCommand maybeRuntimeConnection maybeRuntimeTransaction =
16- let makeSelectCommand connection =
17- let selectCommand = new SqlCommand( " SELECT * FROM " + tableName)
18- selectCommand.Connection <- connection
19- selectCommand
20-
21- if Option.isSome knownSelectCommand
22- then
23- knownSelectCommand.Value
24- else
25- match maybeRuntimeTransaction, maybeRuntimeConnection with
26- | Some ( tran: SqlTransaction), _ ->
27- let command = makeSelectCommand tran.Connection
28- command.Transaction <- tran
29- command
30- | None, Some connection ->
31- makeSelectCommand connection
32- | _ -> makeSelectCommand ( getDesignTimeConnection.Value)
33-
34- new ( tableName , getDesignTimeConnection ) = new DataTable< 'T>( tableName, None, getDesignTimeConnection)
35- new ( createCommand ) = new DataTable< 'T>( null , Some createCommand, lazy ( createCommand.Connection))
3615
3716 member __.Rows : IList < 'T > = {
3817 new IList< 'T> with
@@ -59,9 +38,18 @@ type DataTable<'T when 'T :> DataRow> private (tableName, knownSelectCommand, ge
5938
6039 member __.NewRow (): 'T = downcast base .NewRow()
6140
41+ member private this.IsDirectTable = this.TableName <> null
42+
6243 member this.Update (? connection , ? transaction , ? batchSize ) =
6344
64- let selectCommand = getSelectCommand connection transaction
45+ connection |> Option.iter selectCommand.set_ Connection
46+ transaction |> Option.iter selectCommand.set_ Transaction
47+
48+ if selectCommand.Connection = null && this.IsDirectTable
49+ then
50+ assert ( connectionString.IsSome)
51+ selectCommand.Connection <- new SqlConnection( connectionString.Value.Value)
52+
6553 use dataAdapter = new SqlDataAdapter( selectCommand)
6654 use commandBuilder = new SqlCommandBuilder( dataAdapter)
6755 use __ = dataAdapter.RowUpdating.Subscribe( fun args ->
@@ -88,24 +76,29 @@ type DataTable<'T when 'T :> DataRow> private (tableName, knownSelectCommand, ge
8876 cmd.UpdatedRowSource <- UpdateRowSource.FirstReturnedRecord
8977 )
9078
91- connection |> Option.iter dataAdapter.SelectCommand.set_ Connection
92- transaction |> Option.iter dataAdapter.SelectCommand.set_ Transaction
9379 batchSize |> Option.iter dataAdapter.set_ UpdateBatchSize
9480
9581 dataAdapter.Update( this)
9682
9783 member this.BulkCopy (? connection , ? copyOptions , ? transaction , ? batchSize , ? timeout : TimeSpan ) =
9884
99- let selectCommand = getSelectCommand connection transaction
100- let connection = defaultArg connection selectCommand.Connection
101- use __ = connection.UseLocally()
102- use bulkCopy =
103- new SqlBulkCopy(
104- connection,
105- copyOptions = defaultArg copyOptions SqlBulkCopyOptions.Default,
106- externalTransaction = defaultArg transaction selectCommand.Transaction
107- )
108- bulkCopy.DestinationTableName <- tableName
85+ let conn ' , tran' =
86+ match connection, transaction with
87+ | _, Some( t: SqlTransaction) -> t.Connection, t
88+ | Some c, None -> c, null
89+ | None, None ->
90+ if this.IsDirectTable
91+ then
92+ assert ( connectionString.IsSome)
93+ new SqlConnection( connectionString.Value.Value), null
94+ else
95+ selectCommand.Connection, selectCommand.Transaction
96+
97+ use __ = conn'.UseLocally()
98+ let options = defaultArg copyOptions SqlBulkCopyOptions.Default
99+ use bulkCopy = new SqlBulkCopy( conn', options, tran')
100+ bulkCopy.DestinationTableName <- this.TableName
109101 batchSize |> Option.iter bulkCopy.set_ BatchSize
110102 timeout |> Option.iter ( fun x -> bulkCopy.BulkCopyTimeout <- int x.TotalSeconds)
111- bulkCopy.WriteToServer this
103+ bulkCopy.WriteToServer this
104+
0 commit comments