-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathSqlDataReader.xml
More file actions
1676 lines (1635 loc) · 88 KB
/
SqlDataReader.xml
File metadata and controls
1676 lines (1635 loc) · 88 KB
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="SqlDataReader">
<SqlDataReader>
<summary>
Provides a way of reading a forward-only stream of rows from a SQL Server database. This class cannot be inherited.
</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
To create a <xref:Microsoft.Data.SqlClient.SqlDataReader>, you must call the <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteReader%2A> method of the <xref:Microsoft.Data.SqlClient.SqlCommand> object, instead of directly using a constructor.
While the <xref:Microsoft.Data.SqlClient.SqlDataReader> is being used, the associated <xref:Microsoft.Data.SqlClient.SqlConnection> is busy serving the <xref:Microsoft.Data.SqlClient.SqlDataReader>, and no other operations can be performed on the <xref:Microsoft.Data.SqlClient.SqlConnection> other than closing it. This is the case until the <xref:Microsoft.Data.SqlClient.SqlDataReader.Close%2A> method of the <xref:Microsoft.Data.SqlClient.SqlDataReader> is called. For example, you cannot retrieve output parameters until after you call <xref:Microsoft.Data.SqlClient.SqlDataReader.Close%2A>.
Changes made to a result set by another process or thread while data is being read may be visible to the user of the `SqlDataReader`. However, the precise behavior is timing dependent.
<xref:Microsoft.Data.SqlClient.SqlDataReader.IsClosed%2A> and <xref:Microsoft.Data.SqlClient.SqlDataReader.RecordsAffected%2A> are the only properties that you can call after the <xref:Microsoft.Data.SqlClient.SqlDataReader> is closed. Although the <xref:Microsoft.Data.SqlClient.SqlDataReader.RecordsAffected%2A> property may be accessed while the <xref:Microsoft.Data.SqlClient.SqlDataReader> exists, always call <xref:Microsoft.Data.SqlClient.SqlDataReader.Close%2A> before returning the value of <xref:Microsoft.Data.SqlClient.SqlDataReader.RecordsAffected%2A> to guarantee an accurate return value.
When using sequential access (<xref:System.Data.CommandBehavior.SequentialAccess?displayProperty=nameWithType>), an <xref:System.InvalidOperationException> will be raised if the <xref:Microsoft.Data.SqlClient.SqlDataReader> position is advanced and another read operation is attempted on the previous column.
> [!NOTE]
> For optimal performance, <xref:Microsoft.Data.SqlClient.SqlDataReader> avoids creating unnecessary objects or making unnecessary copies of data. Therefore, multiple calls to methods such as <xref:Microsoft.Data.SqlClient.SqlDataReader.GetValue%2A> return a reference to the same object. Use caution if you are modifying the underlying value of the objects returned by methods such as <xref:Microsoft.Data.SqlClient.SqlDataReader.GetValue%2A>.
## 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 out to the console window. The code then closes the <xref:Microsoft.Data.SqlClient.SqlDataReader>. The <xref:Microsoft.Data.SqlClient.SqlConnection> is closed automatically at the end of the `using` code block.
[!code-csharp[SqlDataReader_Read Example#1](~/../sqlclient/doc/samples/SqlDataReader_Read.cs#1)]
]]></format>
</remarks>
</SqlDataReader>
<Close>
<summary>
Closes the <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" /> object.
</summary>
<remarks>
<format type="text/markdown">< block.
The `Close` method populates the values for output parameters, return values and `RecordsAffected` on the <xref:Microsoft.Data.SqlClient.SqlDataReader> by consuming any pending results. This may be a long operation depending on the amount of data to be consumed. If output values, return values, and `RecordsAffected` are not important to your application, the time to close may be shortened by calling the <xref:Microsoft.Data.SqlClient.SqlCommand.Cancel%2A> method of the associated <xref:Microsoft.Data.SqlClient.SqlCommand> object before the `Close` method is called.
> [!CAUTION]
> Do not call `Close` or `Dispose` on a Connection, a DataReader, or any other managed object in the `Finalize` method of your class. In a finalizer, you should only release unmanaged resources that your class owns directly. If your class does not own any unmanaged resources, do not include a `Finalize` method in your class definition. For more information, see [Garbage Collection](https://learn.microsoft.com/dotnet/standard/garbage-collection/).
## 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 out to the console window. The code then closes the <xref:Microsoft.Data.SqlClient.SqlDataReader>. The <xref:Microsoft.Data.SqlClient.SqlConnection> is closed automatically at the end of the `using` code block.
[!code-csharp[SqlDataReader_Close Example#1](~/../sqlclient/doc/samples/SqlDataReader_Close.cs#1)]
]]></format>
</remarks>
</Close>
<Connection>
<summary>
Gets the <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> associated with the <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" />.
</summary>
<value>
The <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> associated with the <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" />.
</value>
</Connection>
<Depth>
<summary>
Gets a value that indicates the depth of nesting for the current row.
</summary>
<value>
The depth of nesting for the current row.
</value>
<remarks>
The outermost table has a depth of zero. The .NET Framework Data Provider for SQL Server does not support nesting and always returns zero.
</remarks>
</Depth>
<Dispose>
<param name="disposing">
To be added.
</param>
<summary>
To be added.
</summary>
<remarks>
To be added.
</remarks>
</Dispose>
<FieldCount>
<summary>
Gets the number of columns in the current row.
</summary>
<value>
When not positioned in a valid recordset, 0; otherwise the number of columns in the current row. The default is -1.
</value>
<remarks>
Executing a query that, by its nature, does not return rows (such as a DELETE query), sets <see cref="P:Microsoft.Data.SqlClient.SqlDataReader.FieldCount" /> to 0. However. this should not be confused with a query that returns 0 rows (such as <c>SELECT * FROM <table> WHERE 1 = 2</c>) in which case <see cref="P:Microsoft.Data.SqlClient.SqlDataReader.FieldCount" /> returns the number of columns in the table, including hidden fields. Use <see cref="P:Microsoft.Data.SqlClient.SqlDataReader.VisibleFieldCount" /> to exclude hidden fields.
</remarks>
<exception cref="T:System.NotSupportedException">
There is no current connection to an instance of SQL Server.
</exception>
</FieldCount>
<GetBoolean>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a Boolean.
</summary>
<returns>
The value of the column.
</returns>
<remarks>
<para>
No conversions are performed; therefore, the data retrieved must already be a Boolean, or an exception is generated.
</para>
<para>
Call <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> to check for null values before calling this method.
</para>
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetBoolean>
<GetByte>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a byte.
</summary>
<returns>
The value of the specified column as a byte.
</returns>
<remarks>
<para>
No conversions are performed; therefore, the data retrieved must already be a byte.
</para>
<para>
Call <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> to check for null values before calling this method.
</para>
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetByte>
<GetBytes>
<param name="i">
The zero-based column ordinal.
</param>
<param name="dataIndex">
The index within the field from which to begin the read operation.
</param>
<param name="buffer">
The buffer into which to read the stream of bytes.
</param>
<param name="bufferIndex">
The index within the <paramref name="buffer" /> where the write operation is to start.
</param>
<param name="length">
The maximum length to copy into the buffer.
</param>
<summary>
Reads a stream of bytes from the specified column offset into the buffer an array starting at the given buffer offset.
</summary>
<returns>
The actual number of bytes read.
</returns>
<remarks>
<para>
<b>GetBytes</b> returns the number of available bytes in the field. Most of the time this is the exact length of the field. However, the number returned may be less than the true length of the field if <b>GetBytes</b> has already been used to obtain bytes from the field. This may be the case, for example, if the <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" /> is reading a large data structure into a buffer. For more information, see the <see cref="F:System.Data.CommandBehavior.SequentialAccess" /> setting for <see cref="T:System.Data.CommandBehavior" />.
</para>
<para>
If you pass a buffer that is <see langword="null" />, <b>GetBytes</b> returns the length of the entire field in bytes, not the remaining size based on the buffer offset parameter.
</para>
<para>
No conversions are performed; therefore, the data retrieved must already be a byte array.
</para>
</remarks>
</GetBytes>
<GetChar>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a single character.
</summary>
<returns>
The value of the specified column.
</returns>
<remarks>
Not supported for <see cref="T:Microsoft.Data.SqlClient" />.
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetChar>
<GetChars>
<param name="i">
The zero-based column ordinal.
</param>
<param name="dataIndex">
The index within the field from which to begin the read operation.
</param>
<param name="buffer">
The buffer into which to read the stream of bytes.
</param>
<param name="bufferIndex">
The index within the <paramref name="buffer" /> where the write operation is to start.
</param>
<param name="length">
The maximum length to copy into the buffer.
</param>
<summary>
Reads a stream of characters from the specified column offset into the buffer as an array starting at the given buffer offset.
</summary>
<returns>
The actual number of characters read.
</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
<xref:Microsoft.Data.SqlClient.SqlDataReader.GetChars%2A> returns the number of available characters in the field. Frequently this is the exact length of the field. However, the number returned may be less than the true length of the field if `GetChars` has already been used to obtain characters from the field. This may be the case, for example, if the <xref:Microsoft.Data.SqlClient.SqlDataReader> is reading a large data structure into a buffer. For more information, see the `SequentialAccess` setting for <xref:System.Data.CommandBehavior>.
The actual number of characters read can be less than the requested length, if the end of the field is reached. If you pass a buffer that is `null`, <xref:Microsoft.Data.SqlClient.SqlDataReader.GetChars%2A> returns the length of the entire field in characters, not the remaining size based on the buffer offset parameter.
No conversions are performed; therefore. the data retrieved must already be a character array.
> [!NOTE]
> The <xref:Microsoft.Data.SqlClient.SqlDataReader.GetChars%2A> method returns 0 when `dataIndex` is negative.
]]></format>
</remarks>
</GetChars>
<GetColumnSchema>
<summary>
Gets the read-only column schema collection.
</summary>
<returns>
The read-only column schema collection.
</returns>
<remarks>
This method is an implementation of <see cref="M:System.Data.Common.IDbColumnSchemaGenerator.GetColumnSchema" /> method, which enables the use of the <see cref="T:System.Data.Common.IDbColumnSchemaGenerator" /> interface to populate the <see cref="T:System.Data.Common.DbColumn" /> schema metadata without using a <see cref="T:System.Data.DataTable" />.
</remarks>
</GetColumnSchema>
<GetData>
<param name="i">
A column ordinal.
</param>
<summary>
Returns an <see cref="T:System.Data.IDataReader" /> for the specified column ordinal.
</summary>
<returns>
The <see cref="T:System.Data.IDataReader" /> instance for the specified column ordinal.
</returns>
</GetData>
<GetDataTypeName>
<param name="i">
The zero-based ordinal position of the column to find.
</param>
<summary>
Gets a string representing the data type of the specified column.
</summary>
<returns>
The string representing the data type of the specified column.
</returns>
<remarks>
Returns the name of the back-end data type. <c>numeric</c> is a synonym in SQL Server for the <c>decimal</c> data type. <b>GetDataTypeName</b> will return "decimal" for a column defined as either decimal or numeric.
</remarks>
</GetDataTypeName>
<GetDateTime>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.DateTime" /> object.
</summary>
<returns>
The value of the specified column.
</returns>
<remarks>
<para>
No conversions are performed; therefore, the data retrieved must already be a <see cref="T:System.DateTime" /> object.
</para>
<para>
Call <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> to check for null values before calling this method.
</para>
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetDateTime>
<GetDateTimeOffset>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Retrieves the value of the specified column as a <see cref="T:System.DateTimeOffset" /> object.
</summary>
<returns>
The value of the specified column.
</returns>
<remarks>
<para>
No conversions are performed; therefore, the data retrieved must already be a <see cref="T:System.DateTimeOffset" /> object.
</para>
<para>
Call <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> to check for null values before calling this method.
</para>
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetDateTimeOffset>
<GetDecimal>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Decimal" /> object.
</summary>
<returns>
The value of the specified column.
</returns>
<remarks>
<para>
No conversions are performed; therefore, the data retrieved must already be a <see cref="T:System.Decimal" /> object.
</para>
<para>
Call <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> to check for null values before calling this method.
</para>
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetDecimal>
<GetDouble>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a double-precision floating point number.
</summary>
<returns>
The value of the specified column.
</returns>
<remarks>
<para>
No conversions are performed. Therefore, the data retrieved must already be a double-precision floating point number.
</para>
<para>
Call <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> to check for null values before calling this method.
</para>
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetDouble>
<GetEnumerator>
<summary>
Returns an <see cref="T:System.Collections.IEnumerator" /> that iterates through the <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" />.
</summary>
<returns>
An <see cref="T:System.Collections.IEnumerator" /> for the <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" />.
</returns>
<remarks>
Although you can use this method to retrieve an explicit enumerator, in languages that support a <c>foreach</c> construct, it is simpler to use the looping construct directly in order to iterate through the rows in the data reader.
</remarks>
</GetEnumerator>
<GetFieldType>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the <see cref="T:System.Type" /> that is the data type of the object.
</summary>
<returns>
The <see cref="T:System.Type" /> that is the data type of the object. If the type does not exist on the client, in the case of a User-Defined Type (UDT) returned from the database, <b>GetFieldType</b> returns null.
</returns>
</GetFieldType>
<GetFieldValue>
<typeparam name="T">
The type of the value to be returned.
</typeparam>
<param name="i">
The column to be retrieved.
</param>
<summary>
Synchronously gets the value of the specified column as a type. <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.GetFieldValueAsync``1(System.Int32,System.Threading.CancellationToken)" /> is the asynchronous version of this method.
</summary>
<returns>
The returned type object.
</returns>
<remarks>
<para>
<typeparamref name="T" /> can be one of the following types:
</para>
<list type="bullet">
<item><description>Boolean (<c>bool</c>)</description></item>
<item><description>Byte</description></item>
<item><description>Char</description></item>
<item><description>DateOnly (.NET 6 or later)</description></item>
<item><description>DateTime</description></item>
<item><description>DateTimeOffset</description></item>
<item><description>Decimal</description></item>
<item><description>Double</description></item>
<item><description>Single (<c>float</c>)</description></item>
<item><description>Guid</description></item>
<item><description>Int16 (<c>short</c>)</description></item>
<item><description>Int32 (<c>int</c>)</description></item>
<item><description>Int64 (<c>long</c>)</description></item>
<item><description>SqlBoolean</description></item>
<item><description>SqlByte</description></item>
<item><description>SqlDateTime</description></item>
<item><description>SqlDecimal</description></item>
<item><description>SqlDouble</description></item>
<item><description>SqlGuid</description></item>
<item><description>SqlInt16</description></item>
<item><description>SqlInt32</description></item>
<item><description>SqlInt64</description></item>
<item><description>SqlMoney</description></item>
<item><description>SqlSingle</description></item>
<item><description>SqlString</description></item>
<item><description>SqlVector</description></item>
<item><description>Stream</description></item>
<item><description>String</description></item>
<item><description>TextReader</description></item>
<item><description>TimeOnly (.NET 6 or later)</description></item>
<item><description>XmlReader</description></item>
<item><description>UDT, which can be any CLR type marked with <see cref="T:Microsoft.SqlServer.Server.SqlUserDefinedTypeAttribute" />.</description></item>
</list>
<para>
For more information, see <see href="https://learn.microsoft.com/sql/connect/ado-net/sqlclient-streaming-support">SqlClient Streaming Support</see>.
</para>
</remarks>
<exception cref="T:System.InvalidOperationException">
<list type="bullet">
<item><description>The connection drops or is closed during the data retrieval.</description></item>
<item><description>The <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" /> is closed during the data retrieval.</description></item>
<item><description>There is no data ready to be read (for example, the first <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.Read" /> hasn't been called, or returned false).</description></item>
<item><description>Tried to read a previously-read column in sequential mode.</description></item>
<item><description>There was an asynchronous operation in progress. This applies to all Get* methods when running in sequential mode, as they could be called while reading a stream.</description></item>
</list>
</exception>
<exception cref="T:System.IndexOutOfRangeException">
Trying to read a column that does not exist.
</exception>
<exception cref="T:System.Data.SqlTypes.SqlNullValueException">
The value of the column was null (<see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> == <see langword="true" />), retrieving a non-SQL type.
</exception>
<exception cref="T:System.InvalidCastException"><typeparamref name="T" /> doesn't match the type returned by SQL Server or cannot be cast.</exception>
</GetFieldValue>
<GetFieldValueAsync>
<typeparam name="T">
The type of the value to be returned.
</typeparam>
<param name="i">
The column to be retrieved.
</param>
<param name="cancellationToken">
The cancellation instruction, which propagates a notification that operations should be canceled. This does not guarantee the cancellation. A setting of <see langword="CancellationToken.None" /> makes this method equivalent to <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" />. The returned task must be marked as cancelled.
</param>
<summary>
Asynchronously gets the value of the specified column as a type. <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.GetFieldValue``1(System.Int32)" /> is the synchronous version of this method.
</summary>
<returns>
The returned type object.
</returns>
<remarks>
<para>
<typeparamref name="T" /> can be one of the following types:
</para>
<list type="bullet">
<item><description>Boolean (<c>bool</c>)</description></item>
<item><description>Byte</description></item>
<item><description>Char</description></item>
<item><description>DateOnly (.NET 6 or later)</description></item>
<item><description>DateTime</description></item>
<item><description>DateTimeOffset</description></item>
<item><description>Decimal</description></item>
<item><description>Double</description></item>
<item><description>Single (<c>float</c>)</description></item>
<item><description>Guid</description></item>
<item><description>Int16 (<c>short</c>)</description></item>
<item><description>Int32 (<c>int</c>)</description></item>
<item><description>Int64 (<c>long</c>)</description></item>
<item><description>SqlBoolean</description></item>
<item><description>SqlByte</description></item>
<item><description>SqlDateTime</description></item>
<item><description>SqlDecimal</description></item>
<item><description>SqlDouble</description></item>
<item><description>SqlGuid</description></item>
<item><description>SqlInt16</description></item>
<item><description>SqlInt32</description></item>
<item><description>SqlInt64</description></item>
<item><description>SqlMoney</description></item>
<item><description>SqlSingle</description></item>
<item><description>SqlString</description></item>
<item><description>SqlVector</description></item>
<item><description>Stream</description></item>
<item><description>String</description></item>
<item><description>TextReader</description></item>
<item><description>TimeOnly (.NET 6 or later)</description></item>
<item><description>XmlReader</description></item>
<item><description>UDT, which can be any CLR type marked with <see cref="T:Microsoft.SqlServer.Server.SqlUserDefinedTypeAttribute" />.</description></item>
</list>
<para>
For more information, see <see href="https://learn.microsoft.com/sql/connect/ado-net/sqlclient-streaming-support">SqlClient Streaming Support</see>.
</para>
</remarks>
<exception cref="T:System.InvalidOperationException">
<list type="bullet">
<item><description>The connection drops or is closed during the data retrieval.</description></item>
<item><description>The <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" /> is closed during the data retrieval.</description></item>
<item><description>There is no data ready to be read (for example, the first <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.Read" /> hasn't been called, or returned false).</description></item>
<item><description>Tried to read a previously-read column in sequential mode.</description></item>
<item><description>There was an asynchronous operation in progress. This applies to all Get* methods when running in sequential mode, as they could be called while reading a stream.</description></item>
</list>
</exception>
<exception cref="T:System.IndexOutOfRangeException">
Trying to read a column that does not exist.
</exception>
<exception cref="T:System.Data.SqlTypes.SqlNullValueException">
The value of the column was null (<see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> == <see langword="true" />), retrieving a non-SQL type.
</exception>
<exception cref="T:System.InvalidCastException"><typeparamref name="T" /> doesn't match the type returned by SQL Server or cannot be cast.</exception>
</GetFieldValueAsync>
<GetFloat>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a single-precision floating point number.
</summary>
<returns>
The value of the specified column.
</returns>
<remarks>
<para>
No conversions are performed. Therefore, the data retrieved must already be a single-precision floating point number.
</para>
<para>
Call <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> to check for null values before calling this method.
</para>
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetFloat>
<GetGuid>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a globally unique identifier (GUID).
</summary>
<returns>
The value of the specified column.
</returns>
<remarks>
<para>
No conversions are performed; therefore, the data retrieved must already be a GUID.
</para>
<para>
Call <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> to check for null values before calling this method.
</para>
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetGuid>
<GetInt16>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a 16-bit signed integer.
</summary>
<returns>
The value of the specified column.
</returns>
<remarks>
<para>
No conversions are performed; therefore, the data retrieved must already be a 16-bit signed integer.
</para>
<para>
Call <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> to check for null values before calling this method.
</para>
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetInt16>
<GetInt32>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a 32-bit signed integer.
</summary>
<returns>
The value of the specified column.
</returns>
<remarks>
<para>
No conversions are performed; therefore, the data retrieved must already be a 32-bit signed integer.
</para>
<para>
Call <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> to check for null values before calling this method.
</para>
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetInt32>
<GetInt64>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a 64-bit signed integer.
</summary>
<returns>
The value of the specified column.
</returns>
<remarks>
<para>
No conversions are performed; therefore, the data retrieved must already be a 64-bit signed integer.
</para>
<para>
Call <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.IsDBNull(System.Int32)" /> to check for null values before calling this method.
</para>
</remarks>
<exception cref="T:System.InvalidCastException">
The specified cast is not valid.
</exception>
</GetInt64>
<GetName>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the name of the specified column.
</summary>
<returns>
The name of the specified column.
</returns>
</GetName>
<GetOrdinal>
<param name="name">
The name of the column.
</param>
<summary>
Gets the column ordinal, given the name of the column.
</summary>
<returns>
The zero-based column ordinal.
</returns>
<remarks>
<para>
<b>GetOrdinal</b> performs a case-sensitive lookup first. If it fails, a second, case-insensitive search occurs (a case-insensitive comparison is done using the database collation). Unexpected results can occur when comparisons are affected by culture-specific casing rules. For example, in Turkish, the following example yields the wrong results because the file system in Turkish does not use linguistic casing rules for the letter 'i' in "file". The method throws an <see cref="T:System.IndexOutOfRangeException" /> exception if the zero-based column ordinal is not found. <b>GetOrdinal</b> is kana-width insensitive.
</para>
<para>
Because ordinal-based lookups are more efficient than named lookups, it is inefficient to call <b>GetOrdinal</b> within a loop. Save time by calling <b>GetOrdinal</b> once and assigning the results to an integer variable for use within the loop.
</para>
</remarks>
<example>
<para>
The following example demonstrates how to use the <see cref="M:Microsoft.Data.SqlClient.SqlDataReader.GetOrdinal(System.String)" /> method.
</para>
<!-- SqlDataReader_GetOrdinal -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
string str = "Data Source=(local);Initial Catalog=Northwind;"
+ "Integrated Security=SSPI";
ReadGetOrdinal(str);
}
private static void ReadGetOrdinal(string connectionString)
{
string queryString = "SELECT DISTINCT CustomerID FROM dbo.Orders;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call GetOrdinal and assign value to variable.
int customerID = reader.GetOrdinal("CustomerID");
// Use variable with GetString inside of loop.
while (reader.Read())
{
Console.WriteLine("CustomerID={0}", reader.GetString(customerID));
}
// Call Close when done reading.
reader.Close();
}
}
}
</code>
</example>
<exception cref="T:System.IndexOutOfRangeException">
The name specified is not a valid column name.
</exception>
</GetOrdinal>
<GetProviderSpecificFieldType>
<param name="i">
An <see cref="T:System.Int32" /> representing the column ordinal.
</param>
<summary>
Gets an <see langword="Object" /> that is a representation of the underlying provider-specific field type.
</summary>
<returns>
Gets an <see cref="T:System.Object" /> that is a representation of the underlying provider-specific field type.
</returns>
</GetProviderSpecificFieldType>
<GetProviderSpecificValue>
<param name="i">
An <see cref="T:System.Int32" /> representing the column ordinal.
</param>
<summary>
Gets an <see langword="Object" /> that is a representation of the underlying provider specific value.
</summary>
<returns>
An <see cref="T:System.Object" /> that is a representation of the underlying provider specific value.
</returns>
</GetProviderSpecificValue>
<GetProviderSpecificValues>
<param name="values">
An array of <see cref="T:System.Object" /> into which to copy the column values.
</param>
<summary>
Gets an array of objects that are a representation of the underlying provider specific values.
</summary>
<returns>
The array of objects that are a representation of the underlying provider specific values.
</returns>
</GetProviderSpecificValues>
<GetSchemaTable>
<summary>
Returns a <see cref="T:System.Data.DataTable" /> that describes the column metadata of the <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" />.
</summary>
<returns>
A <see cref="T:System.Data.DataTable" /> that describes the column metadata.
</returns>
<remarks>
<format type="text/markdown"><.|
|DataTypeName|Returns a string representing the data type of the specified column.|
|IsAliased|`true`: The column name is an alias.<br /><br /> `false`: The column name is not an alias.|
|IsAutoIncrement|`true`: The column assigns values to new rows in fixed increments.<br /><br /> `false`: The column does not assign values to new rows in fixed increments. The default of this column is `false`.|
|IsColumnSet|`true`: The column is a sparse column that is a member of a column set.|
|IsExpression|`true`: The column is an expression.<br /><br /> `false`: The column is not an expression.|
|IsHidden|`true`: The column is hidden.<br /><br /> `false`: The column is not hidden.|
|IsIdentity|`true`: The column is an identity column.<br /><br /> `false`: The column is not an identity column.|
|IsKey|`true`: The column is one of a set of columns in the rowset that, taken together, uniquely identify the row. The set of columns with `IsKey` set to `true` must uniquely identify a row in the rowset. There is no requirement that this set of columns is a minimal set of columns. This set of columns may be generated from a base table primary key, a unique constraint or a unique index.<br /><br /> `false`: The column is not required to uniquely identify the row.|
|IsLong|`true`: The column contains a Binary Long Object (BLOB) that contains very long data. The definition of very long data is provider-specific.<br /><br /> `false`: The column does not contain a Binary Long Object (BLOB) that contains very long data.|
|IsReadOnly|`true`: The column cannot be modified.<br /><br /> `false`: The column can be modified.|
|IsRowVersion|`true`: The column contains a persistent row identifier that cannot be written to, and has no meaningful value except to identity the row.<br /><br /> `false`: The column does not contain a persistent row identifier that cannot be written to, and has no meaningful value except to identity the row.|
|IsUnique|`true`: Column is of type `timestamp`.<br /><br /> `false`: Column is not of type `timestamp`.|
|NonVersionedProviderType|The type of the column irrespective of the current `Type System Version` specified in the connection string. The returned value is from the <xref:System.Data.SqlDbType> enumeration.|
|NumericPrecision|If `ProviderType` is a numeric data type, this is the maximum precision of the column. The precision depends on the definition of the column. If `ProviderType` is not a numeric data type, this is 255.|
|NumericScale|If `ProviderType` is DBTYPE_DECIMAL or DBTYPE_NUMERIC, the number of digits to the right of the decimal point. Otherwise, this is 255.|
|ProviderSpecificDataType|Returns the provider-specific data type of the column based on the `Type System Version` keyword in the connection string.|
|ProviderType|The indicator of the column's data type. If the data type of the column varies from row to row, this must be Object. This column cannot contain a null value.|
|UdtAssemblyQualifiedName|If the column is a user-defined type (UDT), this is the qualified name of the UDT's assembly as per <xref:System.Type.AssemblyQualifiedName%2A>. If the column is not a UDT, this is null.|
|XmlSchemaCollectionDatabase|The name of the database where the schema collection for this XML instance is located, if the row contains information about an XML column. This value is `null` (`Nothing` in Visual Basic) if the collection is defined within the current database. It is also null if there is no schema collection, in which case the `XmlSchemaCollectionName` and `XmlSchemaCollectionOwningSchema` columns are also null.|
|XmlSchemaCollectionName|The name of the schema collection for this XML instance, if the row contains information about an XML column. This value is `null` (`Nothing` in Visual Basic) if there is no associated schema collection. If the value is null, the `XmlSchemaCollectionDatabase` and `XmlSchemaCollectionOwningSchema` columns are also null.|
|XmlSchemaCollectionOwningSchema|The owning relational schema where the schema collection for this XML instance is located, if the row contains information about an XML column. This value is `null` (`Nothing` in Visual Basic) if the collection is defined within the current database. It is also null if there is no schema collection, in which case the `XmlSchemaCollectionDatabase` and `XmlSchemaCollectionName` columns are also null.|
> [!NOTE]
> To make sure that metadata columns return the correct information, you must call <xref:Microsoft.Data.SqlClient.SqlCommand.ExecuteReader%2A> with the `behavior` parameter set to `KeyInfo`. Otherwise, some of the columns in the schema table may return default, null, or incorrect data.
]]></format>
</remarks>
<exception cref="T:System.InvalidOperationException">
The <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" /> is closed.
</exception>
</GetSchemaTable>
<GetSqlBinary>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlBinary" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlBinary" />.
</returns>
<remarks>
No conversions are performed; therefore the data retrieved must already be a binary structure or an exception is generated.
</remarks>
</GetSqlBinary>
<GetSqlBoolean>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlBoolean" />.
</summary>
<returns>
The value of the column.
</returns>
<remarks>
No conversions are performed; therefore, the data retrieved must already be a Boolean or an exception is generated.
</remarks>
</GetSqlBoolean>
<GetSqlByte>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlByte" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlByte" />.
</returns>
<remarks>
No conversions are performed; therefore the data retrieved must already be a byte, or an exception is generated.
</remarks>
</GetSqlByte>
<GetSqlBytes>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as <see cref="T:System.Data.SqlTypes.SqlBytes" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlBytes" />.
</returns>
</GetSqlBytes>
<GetSqlChars>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as <see cref="T:System.Data.SqlTypes.SqlChars" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlChars" />.
</returns>
</GetSqlChars>
<GetSqlDateTime>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlDateTime" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlDateTime" />.
</returns>
<remarks>
No conversions are performed; therefore, the data retrieved must already be a date/time value, or an exception is generated.
</remarks>
</GetSqlDateTime>
<GetSqlDecimal>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlDecimal" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlDecimal" />.
</returns>
<remarks>
No conversions are performed; therefore, the data retrieved must already be a decimal value, or an exception is generated.
</remarks>
</GetSqlDecimal>
<GetSqlDouble>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlDouble" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlDouble" />.
</returns>
<remarks>
No conversions are performed; therefore, the data retrieved must already be a double-precision floating-point number, or an exception is generated.
</remarks>
</GetSqlDouble>
<GetSqlGuid>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlGuid" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlGuid" />.
</returns>
<remarks>
No conversions are performed; therefore, the data retrieved must already be a GUID, or an exception is generated.
</remarks>
</GetSqlGuid>
<GetSqlInt16>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlInt16" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlInt16" />.
</returns>
<remarks>
No conversions are performed; therefore, the data retrieved must already be a 16-bit signed integer, or an exception is generated.
</remarks>
</GetSqlInt16>
<GetSqlInt32>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlInt32" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlInt32" />.
</returns>
<remarks>
No conversions are performed; therefore the data retrieved must already be a 32-bit signed integer, or an exception is generated.
</remarks>
</GetSqlInt32>
<GetSqlInt64>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlInt64" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlInt64" />.
</returns>
<remarks>
No conversions are performed; therefore, the data retrieved must already be a 64-bit signed integer, or an exception is generated.
</remarks>
</GetSqlInt64>
<GetSqlJson>
<param name="i"></param>
<summary>Gets the value of the specified column as a <see cref="T:Microsoft.Data.SqlTypes.SqlJson"/>.</summary>
<returns>A <see cref="T:Microsoft.Data.SqlTypes.SqlJson"/> object representing the column at the given ordinal.</returns>
<remarks>
No conversions are performed; therefore, the data retrieved must already be a JSON string, or an exception is generated.
</remarks>
</GetSqlJson>
<GetSqlVector>
<param name="i"></param>
<summary>
Gets the value of the specified column as a <see cref="T:Microsoft.Data.SqlTypes.SqlVector`1"/>.
</summary>
<returns>
A <see cref="T:Microsoft.Data.SqlTypes.SqlVector`1"/> object representing the column at the given ordinal.
</returns>
<exception cref="T:System.ArgumentOutOfRangeException">
The index passed was outside the range of 0 to <see cref="P:System.Data.DataTableReader.FieldCount" /> - 1
</exception>
<exception cref="T:System.InvalidOperationException">
An attempt was made to read or access columns in a closed <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" />.
</exception>
<exception cref="T:System.InvalidCastException">
The retrieved data is not compatible with the <see cref="T:Microsoft.Data.SqlTypes.SqlVector`1" /> type.
</exception>
<remarks>
No conversions are performed; therefore, the data retrieved must already be a vector value, or an exception is generated.
</remarks>
</GetSqlVector>
<GetSqlMoney>
<param name="i">
The zero-based column ordinal.
</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlMoney" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlMoney" />.
</returns>
<remarks>
No conversions are performed; therefore, the data retrieved must already be a decimal value, or an exception is generated.
</remarks>