@@ -8,28 +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 > ( knownSelectCommandOrDesignTimeInfo ) =
12- inherit DataTable( match knownSelectCommandOrDesignTimeInfo with | Choice1Of2 ( tableName , _) -> tableName | _ -> null )
11+ type DataTable < 'T when 'T :> DataRow >( selectCommand : SqlCommand , ? connectionString : Lazy < string > ) =
12+ inherit DataTable()
1313
14- let tableName = base .TableName
1514 let rows = base .Rows
16- let getSelectCommand maybeRuntimeConnection maybeRuntimeTransaction =
17- let makeSelectCommand connection =
18- let selectCommand = new SqlCommand( " SELECT * FROM " + tableName)
19- selectCommand.Connection <- connection
20- selectCommand
21-
22- match knownSelectCommandOrDesignTimeInfo with
23- | Choice1Of2 (_, ( getDesignTimeConnection: Lazy<_>)) ->
24- match maybeRuntimeTransaction, maybeRuntimeConnection with
25- | Some ( tran: SqlTransaction), _ ->
26- let command = makeSelectCommand tran.Connection
27- command.Transaction <- tran
28- command
29- | None, Some connection ->
30- makeSelectCommand connection
31- | _ -> makeSelectCommand ( getDesignTimeConnection.Value)
32- | Choice2Of2 knownSelectCommand -> knownSelectCommand
3315
3416 member __.Rows : IList < 'T > = {
3517 new IList< 'T> with
@@ -56,9 +38,18 @@ type DataTable<'T when 'T :> DataRow> (knownSelectCommandOrDesignTimeInfo) =
5638
5739 member __.NewRow (): 'T = downcast base .NewRow()
5840
41+ member private this.IsDirectTable = this.TableName <> null
42+
5943 member this.Update (? connection , ? transaction , ? batchSize ) =
6044
61- 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+
6253 use dataAdapter = new SqlDataAdapter( selectCommand)
6354 use commandBuilder = new SqlCommandBuilder( dataAdapter)
6455 use __ = dataAdapter.RowUpdating.Subscribe( fun args ->
@@ -85,24 +76,29 @@ type DataTable<'T when 'T :> DataRow> (knownSelectCommandOrDesignTimeInfo) =
8576 cmd.UpdatedRowSource <- UpdateRowSource.FirstReturnedRecord
8677 )
8778
88- connection |> Option.iter dataAdapter.SelectCommand.set_ Connection
89- transaction |> Option.iter dataAdapter.SelectCommand.set_ Transaction
9079 batchSize |> Option.iter dataAdapter.set_ UpdateBatchSize
9180
9281 dataAdapter.Update( this)
9382
9483 member this.BulkCopy (? connection , ? copyOptions , ? transaction , ? batchSize , ? timeout : TimeSpan ) =
9584
96- let selectCommand = getSelectCommand connection transaction
97- let connection = defaultArg connection selectCommand.Connection
98- use __ = connection.UseLocally()
99- use bulkCopy =
100- new SqlBulkCopy(
101- connection,
102- copyOptions = defaultArg copyOptions SqlBulkCopyOptions.Default,
103- externalTransaction = defaultArg transaction selectCommand.Transaction
104- )
105- 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
106101 batchSize |> Option.iter bulkCopy.set_ BatchSize
107102 timeout |> Option.iter ( fun x -> bulkCopy.BulkCopyTimeout <- int x.TotalSeconds)
108- bulkCopy.WriteToServer this
103+ bulkCopy.WriteToServer this
104+
0 commit comments