-
Notifications
You must be signed in to change notification settings - Fork 291
/
SqlCommand.xml
3090 lines (2681 loc) · 160 KB
/
SqlCommand.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<docs>
<members name="SqlCommand">
<SqlCommand>
<summary>
Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database. This class cannot be inherited.
</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
When an instance of <xref:Microsoft.Data.SqlClient.SqlCommand> is created, the read/write properties are set to their initial values. For a list of these values, see the <xref:Microsoft.Data.SqlClient.SqlCommand> constructor.
<xref:Microsoft.Data.SqlClient.SqlCommand> features the following methods for executing commands at a SQL Server database:
|Item|Description|
|----------|-----------------|
|<xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteNonQuery%2A>|Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this <xref:Microsoft.Data.SqlClient.SqlCommand>, generally executing commands such as INSERT, DELETE, UPDATE, and SET statements. Each call to <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteNonQuery%2A> must be paired with a call to <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQuery%2A> which finishes the operation, typically on a separate thread.|
|<xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A>|Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this <xref:Microsoft.Data.SqlClient.SqlCommand> and retrieves one or more results sets from the server. Each call to <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> must be paired with a call to <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> which finishes the operation, typically on a separate thread.|
|<xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteXmlReader%2A>|Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this <xref:Microsoft.Data.SqlClient.SqlCommand>. Each call to `BeginExecuteXmlReader` must be paired with a call to `EndExecuteXmlReader`, which finishes the operation, typically on a separate thread, and returns an <xref:System.Xml.XmlReader> object.|
|<xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteReader%2A>|Executes commands that return rows. For increased performance, <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteReader%2A> invokes commands using the Transact-SQL `sp_executesql` system stored procedure. Therefore, <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteReader%2A> might not have the effect that you want if used to execute commands such as Transact-SQL SET statements.|
|<xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery%2A>|Executes commands such as Transact-SQL INSERT, DELETE, UPDATE, and SET statements.|
|<xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteScalar%2A>|Retrieves a single value (for example, an aggregate value) from a database.|
|<xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteXmlReader%2A>|Sends the <xref:Microsoft.Data.SqlClient.SqlCommand.CommandText%2A> to the <xref:Microsoft.Data.SqlClient.SqlCommand.Connection%2A> and builds an <xref:System.Xml.XmlReader> object.|
You can reset the <xref:Microsoft.Data.SqlClient.SqlCommand.CommandText%2A> property and reuse the <xref:Microsoft.Data.SqlClient.SqlCommand> object. However, you must close the <xref:Microsoft.Data.SqlClient.SqlDataReader> before you can execute a new or previous command.
If a <xref:Microsoft.Data.SqlClient.SqlException> is generated by the method executing a <xref:Microsoft.Data.SqlClient.SqlCommand>, the <xref:Microsoft.Data.SqlClient.SqlConnection> remains open when the severity level is 19 or less. When the severity level is 20 or greater, the server ordinarily closes the <xref:Microsoft.Data.SqlClient.SqlConnection>. However, the user can reopen the connection and continue.
> [!NOTE]
> Nameless, also called ordinal, parameters are not supported by the .NET Framework Data Provider for SQL Server.
## Examples
The following example creates a <xref:Microsoft.Data.SqlClient.SqlConnection>, a <xref:Microsoft.Data.SqlClient.SqlCommand>, and a <xref:Microsoft.Data.SqlClient.SqlDataReader>. The example reads through the data, writing it to the console. Finally, the example closes the <xref:Microsoft.Data.SqlClient.SqlDataReader> and then the <xref:Microsoft.Data.SqlClient.SqlConnection> as it exits the `Using` code blocks.
[!code-csharp[SqlCommand Example#1](~/../sqlclient/doc/samples/SqlCommand.cs#1)]
The following sample shows how to create and execute different types of SqlCommand objects.
First you must create the sample database, by executing the following script:
[!code-sql[Setup Database](~/../sqlclient/doc/samples/SqlCommand_Setup.sql#1)]
Next, compile and execute the following:
[!code-csharp[SqlCommand Example#2](~/../sqlclient/doc/samples/SqlCommand_Intro.cs#1)]
]]></format>
</remarks>
</SqlCommand>
<ctor name="default">
<summary>
Initializes a new instance of the
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
class.
</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The base constructor initializes all fields to their default values. The following table shows initial property values for an instance of <xref:Microsoft.Data.SqlClient.SqlCommand>.
|Properties|Initial value|
|----------------|-------------------|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandText%2A>|empty string ("")|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandTimeout%2A>|30|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandType%2A>|`CommandType.Text`|
|<xref:Microsoft.Data.SqlClient.SqlCommand.Connection%2A>|Null|
You can change the value for any of these properties through a separate call to the property.
## Examples
The following example creates a <xref:Microsoft.Data.SqlClient.SqlCommand> and sets the `CommandTimeout` property.
[!code-csharp[Classic WebData IDbCommand_CommandTimeout.cs](~/../sqlclient/doc/samples/IDbCommand_CommandTimeout.cs)]
]]></format>
</remarks>
</ctor>
<ctor name="cmdTextString">
<param name="cmdText">
The text of the query.
</param>
<summary>
Initializes a new instance of the
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
class with the text of the query.
</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
When an instance of <xref:Microsoft.Data.SqlClient.SqlCommand> is created, the following read/write properties are set to initial values.
|Properties|Initial value|
|----------------|-------------------|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandText%2A>|`cmdText`|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandTimeout%2A>|30|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandType%2A>|`CommandType.Text`|
|<xref:Microsoft.Data.SqlClient.SqlCommand.Connection%2A>|null|
You can change the value for any of these properties through a separate call to the property.
## Examples
The following example creates a <xref:Microsoft.Data.SqlClient.SqlCommand>, passing in the connection string and command text.
[!code-csharp[SqlCommand_SqlCommand1](~/../sqlclient/doc/samples/SqlCommand_SqlCommand1.cs#1)]
]]></format>
</remarks>
</ctor>
<ctor name="cmdTextStringAndSqlConnection">
<param name="cmdText">
The text of the query.
</param>
<param name="connection">
A
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
that represents the connection to an instance of SQL Server.
</param>
<summary>
Initializes a new instance of the
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
class with the text of the query and a
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
.
</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The following table shows initial property values for an instance of <xref:Microsoft.Data.SqlClient.SqlCommand>.
|Properties|Initial value|
|----------------|-------------------|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandText%2A>|`cmdText`|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandTimeout%2A>|30|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandType%2A>|`CommandType.Text`|
|<xref:Microsoft.Data.SqlClient.SqlCommand.Connection%2A>|A new <xref:Microsoft.Data.SqlClient.SqlConnection> that is the value for the `connection` parameter.|
You can change the value for any of these parameters by setting the related property.
## Examples
The following example creates a <xref:Microsoft.Data.SqlClient.SqlCommand> and sets some of its properties.
[!code-csharp[SqlCommand_SqlCommand2.cs](~/../sqlclient/doc/samples/SqlCommand_SqlCommand2.cs#1)]
]]></format>
</remarks>
</ctor>
<ctor name="cmdTextStringAndSqlConnectionAndSqlTransaction">
<param name="cmdText">
The text of the query.
</param>
<param name="connection">
A
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
that represents the connection to an instance of SQL Server.
</param>
<param name="transaction">
The
<see cref="T:Microsoft.Data.SqlClient.SqlTransaction" />
in which the
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
executes.
</param>
<summary>
Initializes a new instance of the
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
class with the text of the query, a
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
, and the
<see cref="T:Microsoft.Data.SqlClient.SqlTransaction" />
.
</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The following table shows initial property values for an instance of <xref:Microsoft.Data.SqlClient.SqlCommand>.
|Properties|Initial value|
|----------------|-------------------|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandText%2A>|`cmdText`|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandTimeout%2A>|30|
|<xref:Microsoft.Data.SqlClient.SqlCommand.CommandType%2A>|`CommandType.Text`|
|<xref:Microsoft.Data.SqlClient.SqlCommand.Connection%2A>|A new <xref:Microsoft.Data.SqlClient.SqlConnection> that is the value for the `connection` parameter.|
You can change the value for any of these parameters by setting the related property.
]]></format>
</remarks>
</ctor>
<ctor name="cmdTextStringAndSqlConnectionAndSqlTransactionAndSqlCommandColumnEncryptionSetting">
<param name="cmdText">
The text of the query.
</param>
<param name="connection">
A
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
that represents the connection to an instance of SQL Server.
</param>
<param name="transaction">
The
<see cref="T:Microsoft.Data.SqlClient.SqlTransaction" />
in which the
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
executes.
</param>
<param name="columnEncryptionSetting">
The encryption setting. For more information, see [Always Encrypted](/sql/relational-databases/security/encryption/always-encrypted-database-engine).
</param>
<summary>
Initializes a new instance of the
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
class with specified command text, connection, transaction, and encryption setting.
</summary>
<remarks>
To be added.
</remarks>
</ctor>
<BeginExecuteNonQuery name="default">
<summary>
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
.
</summary>
<returns>
An
<see cref="T:System.IAsyncResult" />
that can be used to poll or wait for results, or both; this value is also needed when invoking
<see cref="M:Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQuery(System.IAsyncResult)" />
, which returns the number of affected rows.
</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteNonQuery%2A> method starts the process of asynchronously executing a Transact-SQL statement or stored procedure that does not return rows, so that other tasks can run concurrently while the statement is executing. When the statement has completed, developers must call the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQuery%2A> method to finish the operation. The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteNonQuery%2A> method returns immediately, but until the code executes the corresponding <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQuery%2A> method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same <xref:Microsoft.Data.SqlClient.SqlCommand> object. Calling the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQuery%2A> before the command's execution is completed causes the <xref:Microsoft.Data.SqlClient.SqlCommand> object to block until the execution is finished.
Note that the command text and parameters are sent to the server synchronously. If a large command or many parameters are sent, this method may block during writes. After the command is sent, the method returns immediately without waiting for an answer from the server--that is, reads are asynchronous.
Because this overload does not support a callback procedure, developers must either poll to determine whether the command has completed, using the <xref:System.IAsyncResult.IsCompleted%2A> property of the <xref:System.IAsyncResult> returned by the <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteNonQuery%2A> method; or wait for the completion of one or more commands using the <xref:System.IAsyncResult.AsyncWaitHandle%2A> property of the returned <xref:System.IAsyncResult>.
This method ignores the <xref:Microsoft.Data.SqlClient.SqlCommand.CommandTimeout%2A> property.
## Examples
The following console application creates updates data within the **AdventureWorks** sample database, doing its work asynchronously. In order to emulate a long-running process, this example inserts a WAITFOR statement in the command text. Normally, you would not take efforts to make your commands run slower, but doing this in this case makes it easier to demonstrate the asynchronous behavior.
[!code-csharp[SqlCommand_BeginExecuteNonQuery](~/../sqlclient/doc/samples/SqlCommand_BeginExecuteNonQuery.cs)]
]]></format>
</remarks>
<exception cref="T:System.InvalidCastException">
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Binary** or **VarBinary** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.Stream" />
. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Char**, **NChar**, **NVarChar**, **VarChar**, or **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.TextReader" />
.
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.Xml.XmlReader" />
.
</exception>
<exception cref="T:Microsoft.Data.SqlClient.SqlException">
Any error that occurred while executing the command text.
-or-
A timeout occurred during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.InvalidOperationException">
The
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
- or -
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" />
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection.
</exception>
<exception cref="T:System.IO.IOException">
An error occurred in a
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.ObjectDisposedException">
The
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object was closed during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
</BeginExecuteNonQuery>
<BeginExecuteNonQuery name="AsyncCallbackAndStateObject">
<param name="callback">
An
<see cref="T:System.AsyncCallback" />
delegate that is invoked when the command's execution has completed. Pass
<see langword="null" />
(
<see langword="Nothing" />
in Microsoft Visual Basic) to indicate that no callback is required.
</param>
<param name="stateObject">
A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback procedure using the
<see cref="P:System.IAsyncResult.AsyncState" />
property.
</param>
<summary>
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
, given a callback procedure and state information.
</summary>
<returns>
An
<see cref="T:System.IAsyncResult" />
that can be used to poll or wait for results, or both; this value is also needed when invoking
<see cref="M:Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQuery(System.IAsyncResult)" />
, which returns the number of affected rows.
</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteNonQuery%2A> method starts the process of asynchronously executing a Transact-SQL statement or stored procedure that does not return rows, so that other tasks can run concurrently while the statement is executing. When the statement has completed, developers must call the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQuery%2A> method to finish the operation. The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteNonQuery%2A> method returns immediately, but until the code executes the corresponding <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQuery%2A> method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same <xref:Microsoft.Data.SqlClient.SqlCommand> object. Calling the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQuery%2A> before the command's execution is completed causes the <xref:Microsoft.Data.SqlClient.SqlCommand> object to block until the execution is finished.
The `callback` parameter lets you specify an <xref:System.AsyncCallback> delegate that is called when the statement has completed. You can call the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQuery%2A> method from within this delegate procedure, or from any other location within your application. In addition, you can pass any object in the `asyncStateObject` parameter, and your callback procedure can retrieve this information using the <xref:System.IAsyncResult.AsyncState%2A> property.
Note that the command text and parameters are sent to the server synchronously. If a large command or many parameters are sent, this method may block during writes. After the command is sent, the method returns immediately without waiting for an answer from the server--that is, reads are asynchronous.
Because the callback procedure executes from within a background thread supplied by the Microsoft .NET common language runtime, it is very important that you take a rigorous approach to handling cross-thread interactions from within your applications. For example, you must not interact with a form's contents from within your callback procedure; should you have to update the form, you must switch back to the form's thread in order to do your work. The example in this topic demonstrates this behavior.
All errors that occur during the execution of the operation are thrown as exceptions in the callback procedure. You must handle the exception in the callback procedure, not in the main application. See the example in this topic for additional information on handling exceptions in the callback procedure.
This method ignores the <xref:Microsoft.Data.SqlClient.SqlCommand.CommandTimeout%2A> property.
## Examples
The following Windows application demonstrates the use of the <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteNonQuery%2A> method, executing a Transact-SQL statement that includes a delay of several seconds (emulating a long-running command).
This example demonstrates many important techniques. This includes calling a method that interacts with the form from a separate thread. In addition, this example demonstrates how you must block users from executing a command multiple times concurrently, and how you must make sure that the form does not close before the callback procedure is called.
To set up this example, create a new Windows application. Put a <xref:System.Windows.Forms.Button> control and a <xref:System.Windows.Forms.Label> control on the form (accepting the default name for each control). Add the following code to the form's class, modifying the connection string as needed for your environment.
[!code-csharp[DataWorks SqlCommand_BeginExecuteNonQueryForm#1](~/../sqlclient/doc/samples/SqlCommand_BeginExecuteNonQueryForm.cs)]
]]></format>
</remarks>
<exception cref="T:System.InvalidCastException">
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Binary** or **VarBinary** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.Stream" />
. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Char**, **NChar**, **NVarChar**, **VarChar**, or **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.TextReader" />
.
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.Xml.XmlReader" />
.
</exception>
<exception cref="T:Microsoft.Data.SqlClient.SqlException">
Any error that occurred while executing the command text.
-or-
A timeout occurred during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.InvalidOperationException">
The
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
- or -
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" />
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection.
</exception>
</BeginExecuteNonQuery>
<BeginExecuteReader name="default">
<summary>
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
, and retrieves one or more result sets from the server.
</summary>
<returns>
An
<see cref="T:System.IAsyncResult" />
that can be used to poll or wait for results, or both; this value is also needed when invoking
<see cref="M:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader(System.IAsyncResult)" />
, which returns a
<see cref="T:Microsoft.Data.SqlClient.SqlDataReader" />
instance that can be used to retrieve the returned rows.
</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> method starts the process of asynchronously executing a Transact-SQL statement or stored procedure that returns rows, so that other tasks can run concurrently while the statement is executing. When the statement has completed, developers must call the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method to finish the operation and retrieve the <xref:Microsoft.Data.SqlClient.SqlDataReader> returned by the command. The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> method returns immediately, but until the code executes the corresponding <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same <xref:Microsoft.Data.SqlClient.SqlCommand> object. Calling the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> before the command's execution is completed causes the <xref:Microsoft.Data.SqlClient.SqlCommand> object to block until the execution is finished.
Note that the command text and parameters are sent to the server synchronously. If a large command or many parameters are sent, this method may block during writes. After the command is sent, the method returns immediately without waiting for an answer from the server--that is, reads are asynchronous. Although command execution is asynchronous, value fetching is still synchronous. This means that calls to <xref:Microsoft.Data.SqlClient.SqlDataReader.Read%2A> may block if more data is required and the underlying network's read operation blocks.
Because this overload does not support a callback procedure, developers must either poll to determine whether the command has completed, using the <xref:System.IAsyncResult.IsCompleted%2A> property of the <xref:System.IAsyncResult> returned by the <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> method; or wait for the completion of one or more commands using the <xref:System.IAsyncResult.AsyncWaitHandle%2A> property of the returned <xref:System.IAsyncResult>.
If you use <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteReader%2A> or <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> to access XML data, SQL Server will return any XML results greater than 2,033 characters in length in multiple rows of 2,033 characters each. To avoid this behavior, use <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteXmlReader%2A> or <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteXmlReader%2A> to read FOR XML queries.
This method ignores the <xref:Microsoft.Data.SqlClient.SqlCommand.CommandTimeout%2A> property.
## Examples
The following console application starts the process of retrieving a data reader asynchronously. While waiting for the results, this simple application sits in a loop, investigating the <xref:System.IAsyncResult.IsCompleted%2A> property value. As soon as the process has completed, the code retrieves the <xref:Microsoft.Data.SqlClient.SqlDataReader> and displays its contents.
[!code-csharp[SqlCommand_BeginExecuteReader#1](~/../sqlclient/doc/samples/SqlCommand_BeginExecuteReader.cs)]
]]></format>
</remarks>
<exception cref="T:System.InvalidCastException">
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Binary** or **VarBinary** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.Stream" />
. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Char**, **NChar**, **NVarChar**, **VarChar**, or **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.TextReader" />
.
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.Xml.XmlReader" />
.
</exception>
<exception cref="T:Microsoft.Data.SqlClient.SqlException">
Any error that occurred while executing the command text.
-or-
A timeout occurred during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.InvalidOperationException">
The
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
- or -
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" />
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection.
</exception>
<exception cref="T:System.IO.IOException">
An error occurred in a
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.ObjectDisposedException">
The
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object was closed during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
</BeginExecuteReader>
<BeginExecuteReader name="CommandBehavior">
<param name="behavior">
One of the
<see cref="T:System.Data.CommandBehavior" />
values, indicating options for statement execution and data retrieval.
</param>
<summary>
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
using one of the
<see cref="T:System.Data.CommandBehavior" />
values.
</summary>
<returns>
An
<see cref="T:System.IAsyncResult" />
that can be used to poll, wait for results, or both; this value is also needed when invoking
<see cref="M:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader(System.IAsyncResult)" />
, which returns a
<see cref="T:Microsoft.Data.SqlClient.SqlDataReader" />
instance that can be used to retrieve the returned rows.
</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> method starts the process of asynchronously executing a Transact-SQL statement or stored procedure that returns rows, so that other tasks can run concurrently while the statement is executing. When the statement has completed, developers must call the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method to finish the operation and retrieve the <xref:Microsoft.Data.SqlClient.SqlDataReader> returned by the command. The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> method returns immediately, but until the code executes the corresponding <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same <xref:Microsoft.Data.SqlClient.SqlCommand> object. Calling the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> before the command's execution is completed causes the <xref:Microsoft.Data.SqlClient.SqlCommand> object to block until the execution is finished.
The `behavior` parameter lets you specify options that control the behavior of the command and its connection. These values can be combined together (using the programming language's `OR` operator); generally, developers use the `CommandBehavior.CloseConnection` value to make sure that the connection is closed by the runtime when the <xref:Microsoft.Data.SqlClient.SqlDataReader> is closed.
Note that the command text and parameters are sent to the server synchronously. If a large command or many parameters are sent, this method may block during writes. After the command is sent, the method returns immediately without waiting for an answer from the server--that is, reads are asynchronous. Although command execution is asynchronous, value fetching is still synchronous. This means that calls to <xref:Microsoft.Data.SqlClient.SqlDataReader.Read%2A> may block if more data is required and the underlying network's read operation blocks.
Because this overload does not support a callback procedure, developers must either poll to determine whether the command has completed, using the <xref:System.IAsyncResult.IsCompleted%2A> property of the <xref:System.IAsyncResult> returned by the <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteNonQuery%2A> method; or wait for the completion of one or more commands using the <xref:System.IAsyncResult.AsyncWaitHandle%2A> property of the returned <xref:System.IAsyncResult>.
If you use <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteReader%2A> or <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> to access XML data, SQL Server returns any XML results greater than 2,033 characters in length in multiple rows of 2,033 characters each. To avoid this behavior, use <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteXmlReader%2A> or <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteXmlReader%2A> to read FOR XML queries.
This method ignores the <xref:Microsoft.Data.SqlClient.SqlCommand.CommandTimeout%2A> property.
## Examples
The following console application starts the process of retrieving a data reader asynchronously. While waiting for the results, this simple application sits in a loop, investigating the <xref:System.IAsyncResult.IsCompleted%2A> property value. Once the process has completed, the code retrieves the <xref:Microsoft.Data.SqlClient.SqlDataReader> and displays its contents.
This example also passes the `CommandBehavior.CloseConnection` and `CommandBehavior.SingleRow` values in the behavior parameter, causing the connection to be closed with the returned <xref:Microsoft.Data.SqlClient.SqlDataReader> is closed, and to optimize for a single row result.
[!code-csharp[SqlCommand_BeginExecuteReaderAsyncSimple](~/../sqlclient/doc/samples/SqlCommand_BeginExecuteReaderAsyncSimple.cs)]
]]></format>
</remarks>
<exception cref="T:System.InvalidCastException">
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Binary** or **VarBinary** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.Stream" />
. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Char**, **NChar**, **NVarChar**, **VarChar**, or **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.TextReader" />
.
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.Xml.XmlReader" />
.
</exception>
<exception cref="T:Microsoft.Data.SqlClient.SqlException">
Any error that occurred while executing the command text.
-or-
A timeout occurred during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.InvalidOperationException">
The
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
- or -
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" />
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection.
</exception>
<exception cref="T:System.IO.IOException">
An error occurred in a
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.ObjectDisposedException">
The
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object was closed during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
</BeginExecuteReader>
<BeginExecuteReader name="AsyncCallbackAndstateObject">
<param name="callback">
An
<see cref="T:System.AsyncCallback" />
delegate that is invoked when the command's execution has completed. Pass
<see langword="null" />
(
<see langword="Nothing" />
in Microsoft Visual Basic) to indicate that no callback is required.
</param>
<param name="stateObject">
A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback procedure using the
<see cref="P:System.IAsyncResult.AsyncState" />
property.
</param>
<summary>
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
and retrieves one or more result sets from the server, given a callback procedure and state information.
</summary>
<returns>
An
<see cref="T:System.IAsyncResult" />
that can be used to poll, wait for results, or both; this value is also needed when invoking
<see cref="M:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader(System.IAsyncResult)" />
, which returns a
<see cref="T:Microsoft.Data.SqlClient.SqlDataReader" />
instance which can be used to retrieve the returned rows.
</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> method starts the process of asynchronously executing a Transact-SQL statement or stored procedure that returns rows, so that other tasks can run concurrently while the statement is executing. When the statement has completed, developers must call the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method to finish the operation and retrieve the <xref:Microsoft.Data.SqlClient.SqlDataReader> returned by the command. The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> method returns immediately, but until the code executes the corresponding <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same <xref:Microsoft.Data.SqlClient.SqlCommand> object. Calling the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> before the command's execution is completed cause the <xref:Microsoft.Data.SqlClient.SqlCommand> object to block until the execution is finished.
The `callback` parameter lets you specify an <xref:System.AsyncCallback> delegate that is called when the statement has completed. You can call the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method from within this delegate procedure, or from any other location within your application. In addition, you can pass any object in the `stateObject` parameter, and your callback procedure can retrieve this information using the <xref:System.IAsyncResult.AsyncState%2A> property.
Note that the command text and parameters are sent to the server synchronously. If a large command or many parameters are sent, this method may block during writes. After the command is sent, the method returns immediately without waiting for an answer from the server--that is, reads are asynchronous. Although command execution is asynchronous, value fetching is still synchronous. This means that calls to <xref:Microsoft.Data.SqlClient.SqlDataReader.Read%2A> may block if more data is required and the underlying network's read operation blocks.
Because the callback procedure executes from within a background thread supplied by the Microsoft .NET runtime, it is very important that you take a rigorous approach to handling cross-thread interactions from within your applications. For example, you must not interact with a form's contents from within your callback procedure; should you have to update the form, you must switch back to the form's thread in order to do your work. The example in this topic demonstrates this behavior.
All errors that occur during the execution of the operation are thrown as exceptions in the callback procedure. You must handle the exception in the callback procedure, not in the main application. See the example in this topic for additional information on handling exceptions in the callback procedure.
If you use <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteReader%2A> or <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> to access XML data, SQL Server returns any XML results greater than 2,033 characters in length in multiple rows of 2,033 characters each. To avoid this behavior, use <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteXmlReader%2A> or <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteXmlReader%2A> to read FOR XML queries.
This method ignores the <xref:Microsoft.Data.SqlClient.SqlCommand.CommandTimeout%2A> property.
## Examples
The following Windows application demonstrates the use of the <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> method, executing a Transact-SQL statement that includes a delay of a few seconds (emulating a long-running command). Because the sample executes the command asynchronously, the form remains responsive while awaiting the results. This example passes the executing <xref:Microsoft.Data.SqlClient.SqlCommand> object as the `stateObject` parameter; doing so makes it simple to retrieve the <xref:Microsoft.Data.SqlClient.SqlCommand> object from within the callback procedure, so that the code can call the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method corresponding to the initial call to <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A>.
This example demonstrates many important techniques. This includes calling a method that interacts with the form from a separate thread. In addition, this example demonstrates how you must block users from executing a command multiple times concurrently, and how you must make sure that the form does not close before the callback procedure is called.
To set up this example, create a new Windows application. Put a <xref:System.Windows.Forms.Button> control, a <xref:System.Windows.Forms.DataGridView> control, and a <xref:System.Windows.Forms.Label> control on the form (accepting the default name for each control). Add the following code to the form's class, modifying the connection string as needed for your environment.
[!code-csharp[SqlCommand_BeginExecuteReaderAsync.cs](~/../sqlclient/doc/samples/SqlCommand_BeginExecuteReaderAsync.cs)]
]]></format>
</remarks>
<exception cref="T:System.InvalidCastException">
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Binary** or **VarBinary** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.Stream" />
. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Char**, **NChar**, **NVarChar**, **VarChar**, or **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.TextReader" />
.
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.Xml.XmlReader" />
.
</exception>
<exception cref="T:Microsoft.Data.SqlClient.SqlException">
Any error that occurred while executing the command text.
-or-
A timeout occurred during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.InvalidOperationException">
The
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
- or -
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" />
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection.
</exception>
<exception cref="T:System.IO.IOException">
An error occurred in a
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.ObjectDisposedException">
The
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object was closed during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
</BeginExecuteReader>
<BeginExecuteReader name="AsyncCallbackAndstateObjectAndCommandBehavior">
<param name="callback">
An
<see cref="T:System.AsyncCallback" />
delegate that is invoked when the command's execution has completed. Pass
<see langword="null" />
(
<see langword="Nothing" />
in Microsoft Visual Basic) to indicate that no callback is required.
</param>
<param name="stateObject">
A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback procedure using the
<see cref="P:System.IAsyncResult.AsyncState" />
property.
</param>
<param name="behavior">
One of the
<see cref="T:System.Data.CommandBehavior" />
values, indicating options for statement execution and data retrieval.
</param>
<summary>
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
, using one of the
<see langword="CommandBehavior" />
values, and retrieving one or more result sets from the server, given a callback procedure and state information.
</summary>
<returns>
An
<see cref="T:System.IAsyncResult" />
that can be used to poll or wait for results, or both; this value is also needed when invoking
<see cref="M:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader(System.IAsyncResult)" />
, which returns a
<see cref="T:Microsoft.Data.SqlClient.SqlDataReader" />
instance which can be used to retrieve the returned rows.
</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> method starts the process of asynchronously executing a Transact-SQL statement or stored procedure that returns rows, so that other tasks can run concurrently while the statement is executing. When the statement has completed, developers must call the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method to finish the operation and retrieve the <xref:Microsoft.Data.SqlClient.SqlDataReader> returned by the command. The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> method returns immediately, but until the code executes the corresponding <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same <xref:Microsoft.Data.SqlClient.SqlCommand> object. Calling the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> before the command's execution is completed causes the <xref:Microsoft.Data.SqlClient.SqlCommand> object to block until the execution is finished.
The `callback` parameter lets you specify an <xref:System.AsyncCallback> delegate that is called when the statement has completed. You can call the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method from within this delegate procedure, or from any other location within your application. In addition, you can pass any object in the `stateObject` parameter, and your callback procedure can retrieve this information using the <xref:System.IAsyncResult.AsyncState%2A> property.
The `behavior` parameter lets you specify options that control the behavior of the command and its connection. These values can be combined together (using the programming language's `Or` operator); generally, developers use the `CloseConnection` value to make sure that the connection is closed by the runtime when the <xref:Microsoft.Data.SqlClient.SqlDataReader> is closed. Developers can also optimize the behavior of the <xref:Microsoft.Data.SqlClient.SqlDataReader> by specifying the `SingleRow` value when it is known in advance that the Transact-SQL statement or stored procedure only returns a single row.
Note that the command text and parameters are sent to the server synchronously. If a large command or many parameters are sent, this method may block during writes. After the command is sent, the method returns immediately without waiting for an answer from the server--that is, reads are asynchronous. Although command execution is asynchronous, value fetching is still synchronous. This means that calls to <xref:Microsoft.Data.SqlClient.SqlDataReader.Read%2A> may block if more data is required and the underlying network's read operation blocks.
Because the callback procedure executes from within a background thread supplied by the Microsoft .NET common language runtime, it is very important that you take a rigorous approach to handling cross-thread interactions from within your applications. For example, you must not interact with a form's contents from within your callback procedure--should you have to update the form, you must switch back to the form's thread in order to do your work. The example in this topic demonstrates this behavior.
All errors that occur during the execution of the operation are thrown as exceptions in the callback procedure. You must handle the exception in the callback procedure, not in the main application. See the example in this topic for additional information on handling exceptions in the callback procedure.
If you use <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteReader%2A> or <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> to access XML data, SQL Server will return any XML results greater than 2,033 characters in length in multiple rows of 2,033 characters each. To avoid this behavior, use <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteXmlReader%2A> or <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteXmlReader%2A> to read FOR XML queries.
This method ignores the <xref:Microsoft.Data.SqlClient.SqlCommand.CommandTimeout%2A> property.
## Examples
The following Windows application demonstrates the use of the <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> method, executing a Transact-SQL statement that includes a delay of a few seconds (emulating a long-running command). Because the sample executes the command asynchronously, the form remains responsive while awaiting the results. This example passes the executing <xref:Microsoft.Data.SqlClient.SqlCommand> object as the `stateObject` parameter; doing so makes it simple to retrieve the <xref:Microsoft.Data.SqlClient.SqlCommand> object from within the callback procedure, so that the code can call the <xref:Microsoft.Data.SqlClient.SqlCommand.EndExecuteReader%2A> method corresponding to the initial call to <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A>.
This example demonstrates many important techniques. This includes calling a method that interacts with the form from a separate thread. In addition, this example demonstrates how you must block users from executing a command multiple times concurrently, and how you must make sure that the form does not close before the callback procedure is called.
To set up this example, create a new Windows application. Put a <xref:System.Windows.Forms.Button> control, a <xref:System.Windows.Forms.DataGridView> control, and a <xref:System.Windows.Forms.Label> control on the form (accepting the default name for each control). Add the following code to the form's class, modifying the connection string as needed for your environment.
This example passes the `CommandBehavior.CloseConnection` value in the `behavior` parameter, causing the returned <xref:Microsoft.Data.SqlClient.SqlDataReader> to automatically close its connection when it is closed.
[!code-csharp[SqlCommand_BeginExecuteReaderAsyncBehavior](~/../sqlclient/doc/samples/SqlCommand_BeginExecuteReaderAsyncBehavior.cs)]
]]></format>
</remarks>
<exception cref="T:System.InvalidCastException">
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Binary** or **VarBinary** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.Stream" />
. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Char**, **NChar**, **NVarChar**, **VarChar**, or **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.TextReader" />
.
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.Xml.XmlReader" />
.
</exception>
<exception cref="T:Microsoft.Data.SqlClient.SqlException">
Any error that occurred while executing the command text.
-or-
A timeout occurred during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.InvalidOperationException">
The
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
- or -
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" />
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection.
</exception>
<exception cref="T:System.IO.IOException">
An error occurred in a
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.ObjectDisposedException">
The
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object was closed during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
</BeginExecuteReader>
<BeginExecuteXmlReader name="default">
<summary>
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this
<see cref="T:Microsoft.Data.SqlClient.SqlCommand" />
and returns results as an
<see cref="T:System.Xml.XmlReader" />
object.
</summary>
<returns>
An
<see cref="T:System.IAsyncResult" />
that can be used to poll or wait for results, or both; this value is also needed when invoking
<see langword="EndExecuteXmlReader" />
, which returns a single XML value.
</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteXmlReader%2A> method starts the process of asynchronously executing a Transact-SQL statement that returns rows as XML, so that other tasks can run concurrently while the statement is executing. When the statement has completed, developers must call the `EndExecuteXmlReader` method to finish the operation and retrieve the XML returned by the command. The <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteXmlReader%2A> method returns immediately, but until the code executes the corresponding `EndExecuteXmlReader` method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same <xref:Microsoft.Data.SqlClient.SqlCommand> object. Calling the `EndExecuteXmlReader` before the command's execution is completed causes the <xref:Microsoft.Data.SqlClient.SqlCommand> object to block until the execution is finished.
The <xref:Microsoft.Data.SqlClient.SqlCommand.CommandText%2A> property ordinarily specifies a Transact-SQL statement with a valid FOR XML clause. However, `CommandText` can also specify a statement that returns `ntext` data that contains valid XML.
A typical <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteXmlReader%2A> query can be formatted as in the following C# example:
```csharp
SqlCommand command = new SqlCommand("SELECT ContactID, FirstName, LastName FROM dbo.Contact FOR XML AUTO, XMLDATA", SqlConn);
```
This method can also be used to retrieve a single-row, single-column result set. In this case, if more than one row is returned, the `EndExecuteXmlReader` method attaches the <xref:System.Xml.XmlReader> to the value on the first row, and discards the rest of the result set.
The multiple active result set (MARS) feature lets multiple actions use the same connection.
Note that the command text and parameters are sent to the server synchronously. If a large command or many parameters are sent, this method may block during writes. After the command is sent, the method returns immediately without waiting for an answer from the server--that is, reads are asynchronous. Although command execution is asynchronous, value fetching is still synchronous.
Because this overload does not support a callback procedure, developers need to either poll to determine whether the command has completed, using the <xref:System.IAsyncResult.IsCompleted%2A> property of the <xref:System.IAsyncResult> returned by the <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteXmlReader%2A> method; or wait for the completion of one or more commands using the <xref:System.IAsyncResult.AsyncWaitHandle%2A> property of the returned <xref:System.IAsyncResult>.
If you use <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteReader%2A> or <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReader%2A> to access XML data, SQL Server returns any XML results greater than 2,033 characters in length in multiple rows of 2,033 characters each. To avoid this behavior, use <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteXmlReader%2A> or <xref:Microsoft.Data.SqlClient.SqlCommand.BeginExecuteXmlReader%2A> to read FOR XML queries.
This method ignores the <xref:Microsoft.Data.SqlClient.SqlCommand.CommandTimeout%2A> property.
## Examples
The following console application starts the process of retrieving XML data asynchronously. While waiting for the results, this simple application sits in a loop, investigating the <xref:System.IAsyncResult.IsCompleted%2A> property value. Once the process has completed, the code retrieves the XML and displays its contents.
[!code-csharp[SqlCommand_BeginExecuteXmlReader#1]((~/../sqlclient/doc/samples/SqlCommand_BeginExecuteXmlReader.cs)]
]]></format>
</remarks>
<exception cref="T:System.InvalidCastException">
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Binary** or **VarBinary** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.Stream" />
. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Char**, **NChar**, **NVarChar**, **VarChar**, or **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.IO.TextReader" />
.
-or-
A
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.SqlDbType" />
other than **Xml** was used when
<see cref="P:Microsoft.Data.SqlClient.SqlParameter.Value" />
was set to
<see cref="T:System.Xml.XmlReader" />
.
</exception>
<exception cref="T:Microsoft.Data.SqlClient.SqlException">
Any error that occurred while executing the command text.
-or-
A timeout occurred during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.InvalidOperationException">
The
<see cref="T:Microsoft.Data.SqlClient.SqlConnection" />
closed or dropped during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
- or -
<see cref="P:Microssoft.Data.SqlClient.SqlCommand.EnableOptimizedParameterBinding" />
is set to true and a parameter with direction Output or InputOutput has been added to the <see cref="P:Microsoft.Data.SqlClient.SqlCommand.Parameters" /> collection.
</exception>
<exception cref="T:System.IO.IOException">
An error occurred in a
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
<exception cref="T:System.ObjectDisposedException">
The
<see cref="T:System.IO.Stream" />
,
<see cref="T:System.Xml.XmlReader" />
or
<see cref="T:System.IO.TextReader" />
object was closed during a streaming operation. For more information about streaming, see [SqlClient Streaming Support](/sql/connect/ado-net/sqlclient-streaming-support).
</exception>
</BeginExecuteXmlReader>
<BeginExecuteXmlReader name="AsyncCallbackAndstateObject">
<param name="callback">
An