Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SqlCommand.cs « SqlClient « Data « System « System.Data « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3e247da7d86d809c56dcc2f50edad923774c8ac8 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
//------------------------------------------------------------------------------
// <copyright file="SqlCommand.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------

namespace System.Data.SqlClient {
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Configuration.Assemblies;
    using System.Data;
    using System.Data.Common;
    using System.Data.ProviderBase;
    using System.Data.Sql;
    using System.Data.SqlTypes;
    using System.Diagnostics;
    using System.Diagnostics.Tracing;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.ConstrainedExecution;
    using System.Runtime.Serialization.Formatters;
    using System.Security.Permissions;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using SysTx = System.Transactions;
    using System.Xml;

    using Microsoft.SqlServer.Server;

    [
    DefaultEvent("RecordsAffected"),
    ToolboxItem(true),
    Designer("Microsoft.VSDesigner.Data.VS.SqlCommandDesigner, " + AssemblyRef.MicrosoftVSDesigner)
    ]
    public sealed class SqlCommand : DbCommand, ICloneable {

        private  static int     _objectTypeCount; // Bid counter
        internal readonly int   ObjectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);

        private string          _commandText;
        private CommandType     _commandType;
        private int             _commandTimeout = ADP.DefaultCommandTimeout;
        private UpdateRowSource _updatedRowSource = UpdateRowSource.Both;
        private bool            _designTimeInvisible;

        /// <summary>
        /// Indicates if the column encryption setting was set at-least once in the batch rpc mode, when using AddBatchCommand.
        /// </summary>
        private bool            _wasBatchModeColumnEncryptionSettingSetOnce;

        /// <summary>
        /// Column Encryption Override. Defaults to SqlConnectionSetting, in which case
        /// it will be Enabled if SqlConnectionOptions.IsColumnEncryptionSettingEnabled = true, Disabled if false.
        /// This may also be used to set other behavior which overrides connection level setting.
        /// </summary>
        private SqlCommandColumnEncryptionSetting _columnEncryptionSetting = SqlCommandColumnEncryptionSetting.UseConnectionSetting;

        internal SqlDependency  _sqlDep;

#if DEBUG
        /// <summary>
        /// Force the client to sleep during sp_describe_parameter_encryption in the function TryFetchInputParameterEncryptionInfo.
        /// </summary>
        private static bool _sleepDuringTryFetchInputParameterEncryptionInfo = false;

        /// <summary>
        /// Force the client to sleep during sp_describe_parameter_encryption in the function RunExecuteReaderTds.
        /// </summary>
        private static bool _sleepDuringRunExecuteReaderTdsForSpDescribeParameterEncryption = false;

        /// <summary>
        /// Force the client to sleep during sp_describe_parameter_encryption after ReadDescribeEncryptionParameterResults.
        /// </summary>
        private static bool _sleepAfterReadDescribeEncryptionParameterResults = false;
#endif

        // devnote: Prepare
        // Against 7.0 Server (Sphinx) a prepare/unprepare requires an extra roundtrip to the server.
        //
        // From 8.0 (Shiloh) and above (Yukon) the preparation can be done as part of the command execution.
        //
        private enum EXECTYPE {
            UNPREPARED,         // execute unprepared commands, all server versions (results in sp_execsql call)
            PREPAREPENDING,     // prepare and execute command, 8.0 and above only  (results in sp_prepexec call)
            PREPARED,           // execute prepared commands, all server versions   (results in sp_exec call)
        }

        // devnotes
        //
        // _hiddenPrepare
        // On 8.0 and above the Prepared state cannot be left. Once a command is prepared it will always be prepared.
        // A change in parameters, commandtext etc (IsDirty) automatically causes a hidden prepare
        //
        // _inPrepare will be set immediately before the actual prepare is done.
        // The OnReturnValue function will test this flag to determine whether the returned value is a _prepareHandle or something else.
        //
        // _prepareHandle - the handle of a prepared command. Apparently there can be multiple prepared commands at a time - a feature that we do not support yet.

        private bool _inPrepare         = false;
        private int  _prepareHandle     = -1;
        private bool _hiddenPrepare     = false;
        private int _preparedConnectionCloseCount = -1;
        private int _preparedConnectionReconnectCount = -1;

        private SqlParameterCollection _parameters;
        private SqlConnection          _activeConnection;
        private bool                   _dirty            = false;               // true if the user changes the commandtext or number of parameters after the command is already prepared
        private EXECTYPE               _execType         = EXECTYPE.UNPREPARED; // by default, assume the user is not sharing a connection so the command has not been prepared
        private _SqlRPC[]              _rpcArrayOf1      = null;                // Used for RPC executes
        private _SqlRPC                _rpcForEncryption = null;                // Used for sp_describe_parameter_encryption RPC executes

        // cut down on object creation and cache all these
        // cached metadata
        private _SqlMetaDataSet _cachedMetaData;
        
        // Last TaskCompletionSource for reconnect task - use for cancellation only
        TaskCompletionSource<object> _reconnectionCompletionSource = null;

#if DEBUG
        static internal int DebugForceAsyncWriteDelay { get; set; }
#endif
        internal bool InPrepare {
            get {
                return _inPrepare;
            }
        }

        /// <summary>
        /// Return if column encryption setting is enabled.
        /// The order in the below if is important since _activeConnection.Parser can throw if the 
        /// underlying tds connection is closed and we don't want to change the behavior for folks
        /// not trying to use transparent parameter encryption i.e. who don't use (SqlCommandColumnEncryptionSetting.Enabled or _activeConnection.IsColumnEncryptionSettingEnabled) here.
        /// </summary>
        internal bool IsColumnEncryptionEnabled {
            get {
                return (_columnEncryptionSetting == SqlCommandColumnEncryptionSetting.Enabled
                                 || (_columnEncryptionSetting == SqlCommandColumnEncryptionSetting.UseConnectionSetting && _activeConnection.IsColumnEncryptionSettingEnabled))
                                 && _activeConnection.Parser != null
                                 && _activeConnection.Parser.IsColumnEncryptionSupported;
            }
        }

        // Cached info for async executions
        private class CachedAsyncState {
            private int           _cachedAsyncCloseCount = -1;    // value of the connection's CloseCount property when the asyncResult was set; tracks when connections are closed after an async operation
            private TaskCompletionSource<object> _cachedAsyncResult     = null;
            private SqlConnection _cachedAsyncConnection = null;  // Used to validate that the connection hasn't changed when end the connection;
            private SqlDataReader _cachedAsyncReader     = null;
            private RunBehavior   _cachedRunBehavior     = RunBehavior.ReturnImmediately;
            private string        _cachedSetOptions      = null;
            private string        _cachedEndMethod       = null;

            internal CachedAsyncState () {
            }

            internal SqlDataReader CachedAsyncReader {
                get {return _cachedAsyncReader;}
            }
            internal  RunBehavior CachedRunBehavior {
                get {return _cachedRunBehavior;}
            }
            internal  string CachedSetOptions {
                get {return _cachedSetOptions;}
            }
            internal bool PendingAsyncOperation {
                get {return (null != _cachedAsyncResult);}
            }
            internal string EndMethodName { 
                get { return _cachedEndMethod; } 
            }

            internal bool IsActiveConnectionValid(SqlConnection activeConnection) {
                return (_cachedAsyncConnection == activeConnection && _cachedAsyncCloseCount == activeConnection.CloseCount);
            }

            [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
            internal void ResetAsyncState() {
                _cachedAsyncCloseCount = -1;
                _cachedAsyncResult     = null;
                if (_cachedAsyncConnection != null) {
                    _cachedAsyncConnection.AsyncCommandInProgress = false;
                    _cachedAsyncConnection = null;
                }
                _cachedAsyncReader     = null;
                _cachedRunBehavior     = RunBehavior.ReturnImmediately;
                _cachedSetOptions      = null;
                _cachedEndMethod       = null;
            }

            internal void SetActiveConnectionAndResult(TaskCompletionSource<object> completion, string endMethod, SqlConnection activeConnection) {
                Debug.Assert(activeConnection != null, "Unexpected null connection argument on SetActiveConnectionAndResult!");
                TdsParser parser = activeConnection.Parser;
                if ((parser == null) || (parser.State == TdsParserState.Closed) || (parser.State == TdsParserState.Broken)) {
                    throw ADP.ClosedConnectionError();
                }

                _cachedAsyncCloseCount = activeConnection.CloseCount;
                _cachedAsyncResult     = completion;
                if (null != activeConnection && !parser.MARSOn) {
                    if (activeConnection.AsyncCommandInProgress)
                        throw SQL.MARSUnspportedOnConnection();
                }
                _cachedAsyncConnection = activeConnection;

                // Should only be needed for non-MARS, but set anyways.
                _cachedAsyncConnection.AsyncCommandInProgress = true;
                _cachedEndMethod = endMethod;
            }

            internal void SetAsyncReaderState (SqlDataReader ds, RunBehavior runBehavior, string optionSettings) {
                _cachedAsyncReader  = ds;
                _cachedRunBehavior  = runBehavior;
                _cachedSetOptions   = optionSettings;
            }
        }

        CachedAsyncState _cachedAsyncState = null;

        private CachedAsyncState cachedAsyncState {
            get {
                if (_cachedAsyncState == null) {
                    _cachedAsyncState = new CachedAsyncState ();
                }
                return  _cachedAsyncState;
            }
        }

        // sql reader will pull this value out for each NextResult call.  It is not cumulative
        // _rowsAffected is cumulative for ExecuteNonQuery across all rpc batches
        internal int _rowsAffected = -1; // rows affected by the command

        // number of rows affected by sp_describe_parameter_encryption.
        // The below line is used only for debug asserts and not exposed publicly or impacts functionality otherwise.
        private int _rowsAffectedBySpDescribeParameterEncryption = -1;

        private SqlNotificationRequest _notification;
        private bool _notificationAutoEnlist = true;            // Notifications auto enlistment is turned on by default

        // transaction support
        private SqlTransaction _transaction;

        private StatementCompletedEventHandler _statementCompletedEventHandler;

        private TdsParserStateObject _stateObj; // this is the TDS session we're using.

        // Volatile bool used to synchronize with cancel thread the state change of an executing
        // command going from pre-processing to obtaining a stateObject.  The cancel synchronization
        // we require in the command is only from entering an Execute* API to obtaining a 
        // stateObj.  Once a stateObj is successfully obtained, cancel synchronization is handled
        // by the stateObject.
        private volatile bool _pendingCancel;

        private bool _batchRPCMode;
        private List<_SqlRPC> _RPCList;
        private _SqlRPC[] _SqlRPCBatchArray;
        private _SqlRPC[] _sqlRPCParameterEncryptionReqArray;
        private List<SqlParameterCollection>  _parameterCollectionList;
        private int     _currentlyExecutingBatch;

        /// <summary>
        /// This variable is used to keep track of which RPC batch's results are being read when reading the results of
        /// describe parameter encryption RPC requests in BatchRPCMode.
        /// </summary>
        private int _currentlyExecutingDescribeParameterEncryptionRPC;

        /// <summary>
        /// A flag to indicate if we have in-progress describe parameter encryption RPC requests.
        /// Reset to false when completed.
        /// </summary>
        private bool _isDescribeParameterEncryptionRPCCurrentlyInProgress;

        /// <summary>
        /// Return the flag that indicates if describe parameter encryption RPC requests are in-progress.
        /// </summary>
        internal bool IsDescribeParameterEncryptionRPCCurrentlyInProgress {
            get {
                return _isDescribeParameterEncryptionRPCCurrentlyInProgress;
            }
        }

        //
        //  Smi execution-specific stuff
        //
        sealed private class CommandEventSink : SmiEventSink_Default {
            private SqlCommand _command;

            internal CommandEventSink( SqlCommand command ) : base( ) {
                _command = command;
            }

            internal override void StatementCompleted( int rowsAffected ) {
                if (Bid.AdvancedOn) {
                    Bid.Trace("<sc.SqlCommand.CommandEventSink.StatementCompleted|ADV> %d#, rowsAffected=%d.\n", _command.ObjectID, rowsAffected);
                }
                _command.InternalRecordsAffected = rowsAffected;

// 




            }

            internal override void BatchCompleted() {
                if (Bid.AdvancedOn) {
                    Bid.Trace("<sc.SqlCommand.CommandEventSink.BatchCompleted|ADV> %d#.\n", _command.ObjectID);
                }
            }

            internal override void ParametersAvailable( SmiParameterMetaData[] metaData, ITypedGettersV3 parameterValues ) {
                if (Bid.AdvancedOn) {
                    Bid.Trace("<sc.SqlCommand.CommandEventSink.ParametersAvailable|ADV> %d# metaData.Length=%d.\n", _command.ObjectID, (null!=metaData)?metaData.Length:-1);

                    if (null != metaData) {
                        for (int i=0; i < metaData.Length; i++) {
                            Bid.Trace("<sc.SqlCommand.CommandEventSink.ParametersAvailable|ADV> %d#, metaData[%d] is %ls%ls\n", 
                                        _command.ObjectID, i, metaData[i].GetType().ToString(), metaData[i].TraceString());
                        }
                    }
                }
                Debug.Assert(SmiContextFactory.Instance.NegotiatedSmiVersion >= SmiContextFactory.YukonVersion);
                _command.OnParametersAvailableSmi( metaData, parameterValues );
            }

            internal override void ParameterAvailable(SmiParameterMetaData metaData, SmiTypedGetterSetter parameterValues, int ordinal)
            {
                if (Bid.AdvancedOn) {
                    if (null != metaData) {
                        Bid.Trace("<sc.SqlCommand.CommandEventSink.ParameterAvailable|ADV> %d#, metaData[%d] is %ls%ls\n", 
                                    _command.ObjectID, ordinal, metaData.GetType().ToString(), metaData.TraceString());
                    }
                }
                Debug.Assert(SmiContextFactory.Instance.NegotiatedSmiVersion >= SmiContextFactory.KatmaiVersion);
                _command.OnParameterAvailableSmi(metaData, parameterValues, ordinal);
            }
        }

        private SmiContext              _smiRequestContext; // context that _smiRequest came from
        private CommandEventSink _smiEventSink;
        private SmiEventSink_DeferedProcessing _outParamEventSink;

        private CommandEventSink EventSink {
            get {
                if ( null == _smiEventSink ) {
                    _smiEventSink = new CommandEventSink( this );
                }

                _smiEventSink.Parent = InternalSmiConnection.CurrentEventSink;
                return _smiEventSink;
            }
        }

        private SmiEventSink_DeferedProcessing OutParamEventSink {
            get {
                if (null == _outParamEventSink) {
                    _outParamEventSink = new SmiEventSink_DeferedProcessing(EventSink);
                }
                else {
                    _outParamEventSink.Parent = EventSink;
                }

                return _outParamEventSink;
            }
        }


        public SqlCommand() : base() {
            GC.SuppressFinalize(this);
        }

        public SqlCommand(string cmdText) : this() {
            CommandText = cmdText;
        }

        public SqlCommand(string cmdText, SqlConnection connection) : this() {
            CommandText = cmdText;
            Connection = connection;
        }

        public SqlCommand(string cmdText, SqlConnection connection, SqlTransaction transaction) : this() {
            CommandText = cmdText;
            Connection = connection;
            Transaction = transaction;
        }

        public SqlCommand(string cmdText, SqlConnection connection, SqlTransaction transaction, SqlCommandColumnEncryptionSetting columnEncryptionSetting) : this() {
            CommandText = cmdText;
            Connection = connection;
            Transaction = transaction;
            _columnEncryptionSetting = columnEncryptionSetting;
        }

        private SqlCommand(SqlCommand from) : this() { // Clone
            CommandText = from.CommandText;
            CommandTimeout = from.CommandTimeout;
            CommandType = from.CommandType;
            Connection = from.Connection;
            DesignTimeVisible = from.DesignTimeVisible;
            Transaction = from.Transaction;
            UpdatedRowSource = from.UpdatedRowSource;
            _columnEncryptionSetting = from.ColumnEncryptionSetting;

            SqlParameterCollection parameters = Parameters;
            foreach(object parameter in from.Parameters) {
                parameters.Add((parameter is ICloneable) ? (parameter as ICloneable).Clone() : parameter);
            }
        }

        [
        DefaultValue(null),
        Editor("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DbCommand_Connection),
        ]
        new public SqlConnection Connection {
            get {
                return _activeConnection;
            }
            set {                
                // Don't allow the connection to be changed while in a async opperation.
                if (_activeConnection != value && _activeConnection != null) { // If new value...
                    if (cachedAsyncState.PendingAsyncOperation) { // If in pending async state, throw.
                        throw SQL.CannotModifyPropertyAsyncOperationInProgress(SQL.Connection);
                    }
                }

                // Check to see if the currently set transaction has completed.  If so,
                // null out our local reference.
                if (null != _transaction && _transaction.Connection == null) {
                    _transaction = null;
                }

                // If the connection has changes, then the request context may have changed as well
                _smiRequestContext = null;

                // Command is no longer prepared on new connection, cleanup prepare status
                if (IsPrepared) {
                    if (_activeConnection != value && _activeConnection != null) {
                        RuntimeHelpers.PrepareConstrainedRegions();
                        try {
#if DEBUG
                            TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
                            RuntimeHelpers.PrepareConstrainedRegions();
                            try {
                                tdsReliabilitySection.Start();
#endif //DEBUG
                                // cleanup
                                Unprepare();
#if DEBUG
                            }
                            finally {
                                tdsReliabilitySection.Stop();
                            }
#endif //DEBUG
                        }
                        catch (System.OutOfMemoryException) {
                            _activeConnection.InnerConnection.DoomThisConnection();
                            throw;
                        }
                        catch (System.StackOverflowException) {
                            _activeConnection.InnerConnection.DoomThisConnection();
                            throw;
                        }
                        catch (System.Threading.ThreadAbortException) {
                            _activeConnection.InnerConnection.DoomThisConnection();
                            throw;
                        }
                        catch (Exception) {
                            // we do not really care about errors in unprepare (may be the old connection went bad)                                        
                        }
                        finally {
                            // clean prepare status (even successfull Unprepare does not do that)
                            _prepareHandle = -1;
                            _execType = EXECTYPE.UNPREPARED;
                        }
                    }
                }

                _activeConnection = value; // 

                Bid.Trace("<sc.SqlCommand.set_Connection|API> %d#, %d#\n", ObjectID, ((null != value) ? value.ObjectID : -1));
            }
        }

        override protected DbConnection DbConnection { // V1.2.3300
            get {
                return Connection;
            }
            set {
                Connection = (SqlConnection)value;
            }
        }

        private SqlInternalConnectionSmi InternalSmiConnection {
            get {
                return (SqlInternalConnectionSmi)_activeConnection.InnerConnection;
            }
        }

        private SqlInternalConnectionTds InternalTdsConnection {
            get {
                return (SqlInternalConnectionTds)_activeConnection.InnerConnection;
            }
        }

        private bool IsShiloh {
            get {
                Debug.Assert(_activeConnection != null, "The active connection is null!");
                if (_activeConnection == null)
                    return false;
                return _activeConnection.IsShiloh;
            }
        }

        [
        DefaultValue(true),
        ResCategoryAttribute(Res.DataCategory_Notification),
        ResDescriptionAttribute(Res.SqlCommand_NotificationAutoEnlist),
        ]
        public bool NotificationAutoEnlist {
            get {
                return _notificationAutoEnlist;
            }
            set {
                _notificationAutoEnlist = value;
            }
         }

        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), // MDAC 90471
        ResCategoryAttribute(Res.DataCategory_Notification),
        ResDescriptionAttribute(Res.SqlCommand_Notification),
        ]
        public SqlNotificationRequest Notification {
            get {
                return _notification;
            }
            set {
                Bid.Trace("<sc.SqlCommand.set_Notification|API> %d#\n", ObjectID);
                _sqlDep = null;
                _notification = value;
            }
        }


        internal SqlStatistics Statistics {
            get {
                if (null != _activeConnection) {
                    if (_activeConnection.StatisticsEnabled) {
                        return _activeConnection.Statistics;
                    }
                }
                return null;
            }
        }

        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ResDescriptionAttribute(Res.DbCommand_Transaction),
        ]
        new public SqlTransaction Transaction {
            get {
                // if the transaction object has been zombied, just return null
                if ((null != _transaction) && (null == _transaction.Connection)) { // MDAC 72720
                    _transaction = null;
                }
                return _transaction;
            }
            set {
                // Don't allow the transaction to be changed while in a async opperation.
                if (_transaction != value && _activeConnection != null) { // If new value...
                    if (cachedAsyncState.PendingAsyncOperation) { // If in pending async state, throw
                        throw SQL.CannotModifyPropertyAsyncOperationInProgress(SQL.Transaction);
                    }
                }

                // 
                Bid.Trace("<sc.SqlCommand.set_Transaction|API> %d#\n", ObjectID);
                _transaction = value;
            }
        }

        override protected DbTransaction DbTransaction { // V1.2.3300
            get {
                return Transaction;
            }
            set {
                Transaction = (SqlTransaction)value;
            }
        }

        [
        DefaultValue(""),
        Editor("Microsoft.VSDesigner.Data.SQL.Design.SqlCommandTextEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
        RefreshProperties(RefreshProperties.All), // MDAC 67707
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DbCommand_CommandText),
        ]
        override public string CommandText { // V1.2.3300, XXXCommand V1.0.5000
            get {
                string value = _commandText;
                return ((null != value) ? value : ADP.StrEmpty);
            }
            set {
                if (Bid.TraceOn) {
                    Bid.Trace("<sc.SqlCommand.set_CommandText|API> %d#, '", ObjectID);
                    Bid.PutStr(value); // Use PutStr to write out entire string
                    Bid.Trace("'\n");
                }
                if (0 != ADP.SrcCompare(_commandText, value)) {
                    PropertyChanging();
                    _commandText = value;
                }
            }
        }

        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.TCE_SqlCommand_ColumnEncryptionSetting),
        ]
        public SqlCommandColumnEncryptionSetting ColumnEncryptionSetting {
            get {
                return _columnEncryptionSetting;
            }
        }

        [
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DbCommand_CommandTimeout),
        ]
        override public int CommandTimeout { // V1.2.3300, XXXCommand V1.0.5000
            get {
                return _commandTimeout;
            }
            set {
                Bid.Trace("<sc.SqlCommand.set_CommandTimeout|API> %d#, %d\n", ObjectID, value);
                if (value < 0) {
                    throw ADP.InvalidCommandTimeout(value);
                }
                if (value != _commandTimeout) {
                    PropertyChanging();
                    _commandTimeout = value;
                }
            }
        }

        public void ResetCommandTimeout() { // V1.2.3300
            if (ADP.DefaultCommandTimeout != _commandTimeout) {
                PropertyChanging();
                _commandTimeout = ADP.DefaultCommandTimeout;
            }
        }

        private bool ShouldSerializeCommandTimeout() { // V1.2.3300
            return (ADP.DefaultCommandTimeout != _commandTimeout);
        }

        [
        DefaultValue(System.Data.CommandType.Text),
        RefreshProperties(RefreshProperties.All),
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DbCommand_CommandType),
        ]
        override public CommandType CommandType { // V1.2.3300, XXXCommand V1.0.5000
            get {
                CommandType cmdType = _commandType;
                return ((0 != cmdType) ? cmdType : CommandType.Text);
            }
            set {
                Bid.Trace("<sc.SqlCommand.set_CommandType|API> %d#, %d{ds.CommandType}\n", ObjectID, (int)value);
                if (_commandType != value) {
                    switch(value) { // @perfnote: Enum.IsDefined
                    case CommandType.Text:
                    case CommandType.StoredProcedure:
                        PropertyChanging();
                        _commandType = value;
                        break;
                    case System.Data.CommandType.TableDirect:
                        throw SQL.NotSupportedCommandType(value);
                    default:
                        throw ADP.InvalidCommandType(value);
                    }
                }
            }
        }

        // @devnote: By default, the cmd object is visible on the design surface (i.e. VS7 Server Tray)
        // to limit the number of components that clutter the design surface,
        // when the DataAdapter design wizard generates the insert/update/delete commands it will
        // set the DesignTimeVisible property to false so that cmds won't appear as individual objects
        [
        DefaultValue(true),
        DesignOnly(true),
        Browsable(false),
        EditorBrowsableAttribute(EditorBrowsableState.Never),
        ]
        public override bool DesignTimeVisible { // V1.2.3300, XXXCommand V1.0.5000
            get {
                return !_designTimeInvisible;
            }
            set {
                _designTimeInvisible = !value;
                TypeDescriptor.Refresh(this); // VS7 208845
            }
        }

        [
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DbCommand_Parameters),
        ]
        new public SqlParameterCollection Parameters {
            get {
                if (null == this._parameters) {
                    // delay the creation of the SqlParameterCollection
                    // until user actually uses the Parameters property
                    this._parameters = new SqlParameterCollection();
                }
                return this._parameters;
            }
        }

        override protected DbParameterCollection DbParameterCollection { // V1.2.3300
            get {
                return Parameters;
            }
        }

        [
        DefaultValue(System.Data.UpdateRowSource.Both),
        ResCategoryAttribute(Res.DataCategory_Update),
        ResDescriptionAttribute(Res.DbCommand_UpdatedRowSource),
        ]
        override public UpdateRowSource UpdatedRowSource { // V1.2.3300, XXXCommand V1.0.5000
            get {
                return _updatedRowSource;
            }
            set {
                switch(value) { // @perfnote: Enum.IsDefined
                case UpdateRowSource.None:
                case UpdateRowSource.OutputParameters:
                case UpdateRowSource.FirstReturnedRecord:
                case UpdateRowSource.Both:
                    _updatedRowSource = value;
                    break;
                default:
                    throw ADP.InvalidUpdateRowSource(value);
                }
            }
        }

        [
        ResCategoryAttribute(Res.DataCategory_StatementCompleted),
        ResDescriptionAttribute(Res.DbCommand_StatementCompleted),
        ]
        public event StatementCompletedEventHandler StatementCompleted {
            add {
                _statementCompletedEventHandler += value;
            }
            remove {
                _statementCompletedEventHandler -= value;
            }
        }

        internal void OnStatementCompleted(int recordCount) { // V1.2.3300
             if (0 <= recordCount) {
                StatementCompletedEventHandler handler = _statementCompletedEventHandler;
                if (null != handler) {
                    try {
                        Bid.Trace("<sc.SqlCommand.OnStatementCompleted|INFO> %d#, recordCount=%d\n", ObjectID, recordCount);
                        handler(this, new StatementCompletedEventArgs(recordCount));
                    }
                    catch(Exception e) {
                        // 
                        if (!ADP.IsCatchableOrSecurityExceptionType(e)) {
                            throw;
                        }

                        ADP.TraceExceptionWithoutRethrow(e);
                    }
                }
            }
        }

        private void PropertyChanging() { // also called from SqlParameterCollection
            this.IsDirty = true;
        }

        override public void Prepare() {
            SqlConnection.ExecutePermission.Demand();

            // Reset _pendingCancel upon entry into any Execute - used to synchronize state
            // between entry into Execute* API and the thread obtaining the stateObject.
            _pendingCancel = false;

            // Context connection's prepare is a no-op
            if (null != _activeConnection && _activeConnection.IsContextConnection) {
                return;
            }

            SqlStatistics statistics = null;
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<sc.SqlCommand.Prepare|API> %d#", ObjectID);
            Bid.CorrelationTrace("<sc.SqlCommand.Prepare|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            statistics = SqlStatistics.StartTimer(Statistics);

            // only prepare if batch with parameters
            // MDAC 
            if (
                this.IsPrepared && !this.IsDirty
                || (this.CommandType == CommandType.StoredProcedure)
                ||  (
                        (System.Data.CommandType.Text == this.CommandType)
                        && (0 == GetParameterCount (_parameters))
                    )

            ) {
                if (null != Statistics) {
                    Statistics.SafeIncrement (ref Statistics._prepares);
                }
                _hiddenPrepare = false;
            }
            else {
                // Validate the command outside of the try\catch to avoid putting the _stateObj on error
                ValidateCommand(ADP.Prepare, false /*not async*/);

                bool processFinallyBlock = true;
                TdsParser bestEffortCleanupTarget = null;
                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);

                    // NOTE: The state object isn't actually needed for this, but it is still here for back-compat (since it does a bunch of checks)
                    GetStateObject();

                    // Loop through parameters ensuring that we do not have unspecified types, sizes, scales, or precisions
                    if (null != _parameters) {
                        int count = _parameters.Count;
                        for (int i = 0; i < count; ++i) {
                            _parameters[i].Prepare(this); // MDAC 67063
                        }
                    }

#if DEBUG
                    TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try {
                        tdsReliabilitySection.Start();
#else
                    {
#endif //DEBUG
                        InternalPrepare();
                    }
#if DEBUG
                    finally {
                        tdsReliabilitySection.Stop();
                    }
#endif //DEBUG
                }
                catch (System.OutOfMemoryException e) {
                    processFinallyBlock = false;
                    _activeConnection.Abort(e);
                    throw;
                }
                catch (System.StackOverflowException e) {
                    processFinallyBlock = false;
                    _activeConnection.Abort(e);
                    throw;
                }
                catch (System.Threading.ThreadAbortException e)  {
                    processFinallyBlock = false;
                    _activeConnection.Abort(e);

                    SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                    throw;
                }
                catch (Exception e) {
                    processFinallyBlock = ADP.IsCatchableExceptionType(e);
                    throw;
                }
                finally {
                    if (processFinallyBlock) {
                        _hiddenPrepare = false; // The command is now officially prepared
    
                        ReliablePutStateObject();
                    }
                }
            }

            SqlStatistics.StopTimer(statistics);
            Bid.ScopeLeave(ref hscp);
        }

        private void InternalPrepare() {
            if (this.IsDirty) {
                Debug.Assert(_cachedMetaData == null || !_dirty, "dirty query should not have cached metadata!"); // can have cached metadata if dirty because of parameters
                //
                // someone changed the command text or the parameter schema so we must unprepare the command
                //
                this.Unprepare();
                this.IsDirty = false;
            }
            Debug.Assert(_execType != EXECTYPE.PREPARED, "Invalid attempt to Prepare already Prepared command!");
            Debug.Assert(_activeConnection != null, "must have an open connection to Prepare");
            Debug.Assert(null != _stateObj, "TdsParserStateObject should not be null");
            Debug.Assert(null != _stateObj.Parser, "TdsParser class should not be null in Command.Execute!");
            Debug.Assert(_stateObj.Parser == _activeConnection.Parser, "stateobject parser not same as connection parser");
            Debug.Assert(false == _inPrepare, "Already in Prepare cycle, this.inPrepare should be false!");

            // remember that the user wants to do a prepare but don't actually do an rpc
            _execType = EXECTYPE.PREPAREPENDING;
            // Note the current close count of the connection - this will tell us if the connection has been closed between calls to Prepare() and Execute
            _preparedConnectionCloseCount = _activeConnection.CloseCount;
            _preparedConnectionReconnectCount = _activeConnection.ReconnectCount;

            if (null != Statistics) {
                Statistics.SafeIncrement(ref Statistics._prepares);
            }
        }

        // SqlInternalConnectionTds needs to be able to unprepare a statement
        internal void Unprepare() {
            // Context connection's prepare is a no-op
            if (_activeConnection.IsContextConnection) {
                return;
            }

            Debug.Assert(true == IsPrepared, "Invalid attempt to Unprepare a non-prepared command!");
            Debug.Assert(_activeConnection != null, "must have an open connection to UnPrepare");
            Debug.Assert(false == _inPrepare, "_inPrepare should be false!");

            // @devnote: we're always falling back to Prepare pending
            // @devnote: This seems broken because once the command is prepared it will - always - be a
            // @devnote: prepared execution.
            // @devnote: Even replacing the parameterlist with something completely different or
            // @devnote: changing the commandtext to a non-parameterized query will result in prepared execution
            // @devnote:
            // @devnote: We need to keep the behavior for backward compatibility though (non-breaking change)
            //
            _execType = EXECTYPE.PREPAREPENDING;
            // Don't zero out the handle because we'll pass it in to sp_prepexec on the next prepare
            // Unless the close count isn't the same as when we last prepared
            if ((_activeConnection.CloseCount != _preparedConnectionCloseCount) || (_activeConnection.ReconnectCount != _preparedConnectionReconnectCount)) {
                // reset our handle
                _prepareHandle = -1;
            }

            _cachedMetaData = null;
            Bid.Trace("<sc.SqlCommand.Prepare|INFO> %d#, Command unprepared.\n", ObjectID);
        }


        // Cancel is supposed to be multi-thread safe.
        // It doesn't make sense to verify the connection exists or that it is open during cancel
        // because immediately after checkin the connection can be closed or removed via another thread.
        //
        override public void Cancel() {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<sc.SqlCommand.Cancel|API> %d#", ObjectID);
            
            Bid.CorrelationTrace("<sc.SqlCommand.Cancel|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);

            SqlStatistics statistics = null;
            try {
                statistics = SqlStatistics.StartTimer(Statistics);

                // If we are in reconnect phase simply cancel the waiting task
                var reconnectCompletionSource = _reconnectionCompletionSource;
                if (reconnectCompletionSource != null) {
                    if (reconnectCompletionSource.TrySetCanceled()) {                        
                        return;
                    }
                }
                
                // the pending data flag means that we are awaiting a response or are in the middle of proccessing a response
                // if we have no pending data, then there is nothing to cancel
                // if we have pending data, but it is not a result of this command, then we don't cancel either.  Note that
                // this model is implementable because we only allow one active command at any one time.  This code
                // will have to change we allow multiple outstanding batches

                // 
                if (null == _activeConnection) {
                    return;
                }
                SqlInternalConnectionTds connection = (_activeConnection.InnerConnection as SqlInternalConnectionTds);
                if (null == connection) {  // Fail with out locking
                     return;
                }

                // The lock here is to protect against the command.cancel / connection.close race condition
                // The SqlInternalConnectionTds is set to OpenBusy during close, once this happens the cast below will fail and 
                // the command will no longer be cancelable.  It might be desirable to be able to cancel the close opperation, but this is
                // outside of the scope of Whidbey RTM.  See (SqlConnection::Close) for other lock.
                lock (connection) {                                              
                    if (connection != (_activeConnection.InnerConnection as SqlInternalConnectionTds)) { // make sure the connection held on the active connection is what we have stored in our temp connection variable, if not between getting "connection" and takeing the lock, the connection has been closed
                        return;
                    }
                    
                    TdsParser parser = connection.Parser;
                    if (null == parser) {
                        return;
                    }

                    TdsParser bestEffortCleanupTarget = null;
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try {
#if DEBUG
                        TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                        RuntimeHelpers.PrepareConstrainedRegions();
                        try {
                            tdsReliabilitySection.Start();
#else
                        {
#endif //DEBUG
                            bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);

                            if (!_pendingCancel) { // Do nothing if aleady pending.
                                // Before attempting actual cancel, set the _pendingCancel flag to false.
                                // This denotes to other thread before obtaining stateObject from the
                                // session pool that there is another thread wishing to cancel.
                                // The period in question is between entering the ExecuteAPI and obtaining 
                                // a stateObject.
                                _pendingCancel = true;

                                TdsParserStateObject stateObj = _stateObj;
                                if (null != stateObj) {
                                    stateObj.Cancel(ObjectID);
                                }
                                else {
                                    SqlDataReader reader = connection.FindLiveReader(this);
                                    if (reader != null) {
                                        reader.Cancel(ObjectID);
                                    }
                                }
                            }
                        }
#if DEBUG
                        finally {
                            tdsReliabilitySection.Stop();
                        }
#endif //DEBUG
                    }
                    catch (System.OutOfMemoryException e) {
                        _activeConnection.Abort(e);
                        throw;
                    }
                    catch (System.StackOverflowException e) {
                        _activeConnection.Abort(e);
                        throw;
                    }
                    catch (System.Threading.ThreadAbortException e)  {
                        _activeConnection.Abort(e);
                        SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                        throw;
                    }
                }
            }
            finally {
                SqlStatistics.StopTimer(statistics);
                Bid.ScopeLeave(ref hscp);
            }
        }

        new public SqlParameter CreateParameter() {
            return new SqlParameter();
        }

        override protected DbParameter CreateDbParameter() {
            return CreateParameter();
        }

        override protected void Dispose(bool disposing) {
            if (disposing) { // release mananged objects

                // V1.0, V1.1 did not reset the Connection, Parameters, CommandText, WebData 100524
                //_parameters = null;
                //_activeConnection = null;
                //_statistics = null;
                //CommandText = null;
                _cachedMetaData = null;
            }
            // release unmanaged objects
            base.Dispose(disposing);
        }

        override public object ExecuteScalar() {
            SqlConnection.ExecutePermission.Demand();

            // Reset _pendingCancel upon entry into any Execute - used to synchronize state
            // between entry into Execute* API and the thread obtaining the stateObject.
            _pendingCancel = false;

            SqlStatistics statistics = null;
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<sc.SqlCommand.ExecuteScalar|API> %d#", ObjectID);
            Bid.CorrelationTrace("<sc.SqlCommand.ExecuteScalar|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);

            bool success = false;
            int? sqlExceptionNumber = null;
            try {
                statistics = SqlStatistics.StartTimer(Statistics);
                WriteBeginExecuteEvent();
                SqlDataReader ds;
                ds = RunExecuteReader(0, RunBehavior.ReturnImmediately, true, ADP.ExecuteScalar);
                object result = CompleteExecuteScalar(ds, false);
                success = true;
                return result;
            }
            catch (SqlException ex) {
                sqlExceptionNumber = ex.Number;
                throw;
            }
            finally {
                SqlStatistics.StopTimer(statistics);
                Bid.ScopeLeave(ref hscp);
                WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous: true);
            }
        }

        private object CompleteExecuteScalar(SqlDataReader ds, bool returnSqlValue) {
            object retResult = null;

            try {
                if (ds.Read()) {
                    if (ds.FieldCount > 0) {
                        if (returnSqlValue) {
                            retResult = ds.GetSqlValue(0);
                        }
                        else {
                            retResult = ds.GetValue(0);
                        }
                    }
                }
            }
            finally {
                // clean off the wire
                ds.Close();
            }

            return retResult;
        }

        override public int ExecuteNonQuery() {
            SqlConnection.ExecutePermission.Demand();

            // Reset _pendingCancel upon entry into any Execute - used to synchronize state
            // between entry into Execute* API and the thread obtaining the stateObject.
            _pendingCancel = false;

            SqlStatistics statistics = null;
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<sc.SqlCommand.ExecuteNonQuery|API> %d#", ObjectID);
            Bid.CorrelationTrace("<sc.SqlCommand.ExecuteNonQuery|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            bool success = false;
            int? sqlExceptionNumber = null;
            try {
                statistics = SqlStatistics.StartTimer(Statistics);
                WriteBeginExecuteEvent();
                InternalExecuteNonQuery(null, ADP.ExecuteNonQuery, false, CommandTimeout);
                success = true;
                return _rowsAffected;
            }
            catch (SqlException ex) {
                sqlExceptionNumber = ex.Number;
                throw;
            }
            finally {
                SqlStatistics.StopTimer(statistics);
                Bid.ScopeLeave(ref hscp);
                WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous : true);
            }
        }

        // Handles in-proc execute-to-pipe functionality
        //  Identical to ExecuteNonQuery
        internal void ExecuteToPipe( SmiContext pipeContext ) {
            SqlConnection.ExecutePermission.Demand();

            // Reset _pendingCancel upon entry into any Execute - used to synchronize state
            // between entry into Execute* API and the thread obtaining the stateObject.
            _pendingCancel = false;

            SqlStatistics statistics = null;
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<sc.SqlCommand.ExecuteToPipe|INFO> %d#", ObjectID);
            try {
                statistics = SqlStatistics.StartTimer(Statistics);
                InternalExecuteNonQuery(null, ADP.ExecuteNonQuery, true, CommandTimeout);
            }
            finally {
                SqlStatistics.StopTimer(statistics);
                Bid.ScopeLeave(ref hscp);
            }
        }

        [System.Security.Permissions.HostProtectionAttribute(ExternalThreading=true)]
        public IAsyncResult BeginExecuteNonQuery() {
            // BeginExecuteNonQuery will track ExecutionTime for us
            return BeginExecuteNonQuery(null, null);
        }

        [System.Security.Permissions.HostProtectionAttribute(ExternalThreading=true)]
        public IAsyncResult BeginExecuteNonQuery(AsyncCallback callback, object stateObject) {
            Bid.CorrelationTrace("<sc.SqlCommand.BeginExecuteNonQuery|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            SqlConnection.ExecutePermission.Demand();
            return BeginExecuteNonQueryInternal(callback, stateObject, 0);
        }

        private IAsyncResult BeginExecuteNonQueryAsync(AsyncCallback callback, object stateObject) {
            return BeginExecuteNonQueryInternal(callback, stateObject, CommandTimeout, asyncWrite:true);
        }

        private IAsyncResult BeginExecuteNonQueryInternal(AsyncCallback callback, object stateObject, int timeout, bool asyncWrite = false) {
            // Reset _pendingCancel upon entry into any Execute - used to synchronize state
            // between entry into Execute* API and the thread obtaining the stateObject.
            _pendingCancel = false;

            ValidateAsyncCommand(); // Special case - done outside of try/catches to prevent putting a stateObj
                                    // back into pool when we should not.
            
            SqlStatistics statistics = null;
            try {
                statistics = SqlStatistics.StartTimer(Statistics);
                WriteBeginExecuteEvent();
                TaskCompletionSource<object> completion = new TaskCompletionSource<object>(stateObject);

                try { // InternalExecuteNonQuery already has reliability block, but if failure will not put stateObj back into pool.
                    Task execNQ = InternalExecuteNonQuery(completion, ADP.BeginExecuteNonQuery, false, timeout, asyncWrite);
                    if (execNQ != null) {
                        AsyncHelper.ContinueTask(execNQ, completion, () => BeginExecuteNonQueryInternalReadStage(completion));
                    }
                    else {
                        BeginExecuteNonQueryInternalReadStage(completion);
                    }
                }
                catch (Exception e) {
                    if (!ADP.IsCatchableOrSecurityExceptionType(e)) {
                        // If not catchable - the connection has already been caught and doomed in RunExecuteReader.
                        throw;
                    }

                    // For async, RunExecuteReader will never put the stateObj back into the pool, so do so now.
                    ReliablePutStateObject(); 
                    throw;
                }

                // Add callback after work is done to avoid overlapping Begin\End methods
                if (callback != null) {
                    completion.Task.ContinueWith((t) => callback(t), TaskScheduler.Default);
                }

                return completion.Task;
            }
            finally {
                SqlStatistics.StopTimer(statistics);
            }
        }

        private void BeginExecuteNonQueryInternalReadStage(TaskCompletionSource<object> completion) {
            // Read SNI does not have catches for async exceptions, handle here.
            TdsParser bestEffortCleanupTarget = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                    {
#endif //DEBUG
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
                    // must finish caching information before ReadSni which can activate the callback before returning
                    cachedAsyncState.SetActiveConnectionAndResult(completion, ADP.EndExecuteNonQuery, _activeConnection);
                    _stateObj.ReadSni(completion);
                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif //DEBUG
            }
            catch (System.OutOfMemoryException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.StackOverflowException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.Threading.ThreadAbortException e) {
                _activeConnection.Abort(e);
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                throw;
            }
            catch (Exception) {
                // Similarly, if an exception occurs put the stateObj back into the pool.
                // and reset async cache information to allow a second async execute
                if (null != _cachedAsyncState) {
                    _cachedAsyncState.ResetAsyncState();
                }
                ReliablePutStateObject();
                throw;
            }
        }

        private void VerifyEndExecuteState(Task completionTask, String endMethod) {
            if (null == completionTask) {
                throw ADP.ArgumentNull("asyncResult");
            }
            if (completionTask.IsCanceled) {
                if (_stateObj != null) {
                    _stateObj.Parser.State = TdsParserState.Broken; // We failed to respond to attention, we have to quit!
                    _stateObj.Parser.Connection.BreakConnection();
                    _stateObj.Parser.ThrowExceptionAndWarning(_stateObj);
                }
                else {
                    Debug.Assert(_reconnectionCompletionSource == null || _reconnectionCompletionSource.Task.IsCanceled, "ReconnectCompletionSource should be null or cancelled");
                    throw SQL.CR_ReconnectionCancelled();
                }
            }
            else if (completionTask.IsFaulted) {
                throw completionTask.Exception.InnerException;
            }

            // If transparent parameter encryption was attempted, then we need to skip other checks like those on EndMethodName
            // since we want to wait for async results before checking those fields.
            if (IsColumnEncryptionEnabled) {
                if (_activeConnection.State != ConnectionState.Open) {
                    // If the connection is not 'valid' then it was closed while we were executing
                    throw ADP.ClosedConnectionError();
                }

                return;
            }

            if (cachedAsyncState.EndMethodName == null) {
                throw ADP.MethodCalledTwice(endMethod);
            }
            if (endMethod != cachedAsyncState.EndMethodName) {
                throw ADP.MismatchedAsyncResult(cachedAsyncState.EndMethodName, endMethod);
            }
            if ((_activeConnection.State != ConnectionState.Open) || (!cachedAsyncState.IsActiveConnectionValid(_activeConnection))) {
                // If the connection is not 'valid' then it was closed while we were executing
                throw ADP.ClosedConnectionError();
            }
        }

        private void WaitForAsyncResults(IAsyncResult asyncResult) {
            Task completionTask = (Task) asyncResult;
            if (!asyncResult.IsCompleted) {
                asyncResult.AsyncWaitHandle.WaitOne();
            }
            _stateObj._networkPacketTaskSource = null;
            _activeConnection.GetOpenTdsConnection().DecrementAsyncCount();
        }

        public int EndExecuteNonQuery(IAsyncResult asyncResult) {
            try {
                return EndExecuteNonQueryInternal(asyncResult);
            } 
            finally {
                Bid.CorrelationTrace("<sc.SqlCommand.EndExecuteNonQuery|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            }
        }

        private void ThrowIfReconnectionHasBeenCanceled() {
            if (_stateObj == null) {
                var reconnectionCompletionSource = _reconnectionCompletionSource;
                if (reconnectionCompletionSource != null && reconnectionCompletionSource.Task.IsCanceled) {
                    throw SQL.CR_ReconnectionCancelled();
                }
            }
        }
        
        private int EndExecuteNonQueryAsync(IAsyncResult asyncResult) {
            Bid.CorrelationTrace("<sc.SqlCommand.EndExecuteNonQueryAsync|Info|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);

            Exception asyncException = ((Task)asyncResult).Exception;
            if (asyncException != null) {
                // Leftover exception from the Begin...InternalReadStage
                ReliablePutStateObject();
                throw asyncException.InnerException;
            }
            else {
                ThrowIfReconnectionHasBeenCanceled();
                // lock on _stateObj prevents ----s with close/cancel.
                lock (_stateObj) {
                    return EndExecuteNonQueryInternal(asyncResult);
                }
            }
        }

        private int EndExecuteNonQueryInternal(IAsyncResult asyncResult) {
            SqlStatistics statistics = null;

            TdsParser bestEffortCleanupTarget = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            bool success = false;
            int? sqlExceptionNumber = null;
            try {
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                {
#endif //DEBUG
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
                    statistics = SqlStatistics.StartTimer(Statistics);
                    VerifyEndExecuteState((Task)asyncResult, ADP.EndExecuteNonQuery);
                    WaitForAsyncResults(asyncResult);

                    // If Transparent parameter encryption was attempted, then we would have skipped the below 
                    // checks in VerifyEndExecuteState since we wanted to wait for WaitForAsyncResults to complete.
                    if (IsColumnEncryptionEnabled) {
                        if (cachedAsyncState.EndMethodName == null) {
                            throw ADP.MethodCalledTwice(ADP.EndExecuteNonQuery);
                        }

                        if (ADP.EndExecuteNonQuery != cachedAsyncState.EndMethodName) {
                            throw ADP.MismatchedAsyncResult(cachedAsyncState.EndMethodName, ADP.EndExecuteNonQuery);
                        }

                        if (!cachedAsyncState.IsActiveConnectionValid(_activeConnection)) {
                            // If the connection is not 'valid' then it was closed while we were executing
                            throw ADP.ClosedConnectionError();
                        }
                    }

                    bool processFinallyBlock = true;
                    try {
                        NotifyDependency();
                        CheckThrowSNIException();

                        // only send over SQL Batch command if we are not a stored proc and have no parameters
                        if ((System.Data.CommandType.Text == this.CommandType) && (0 == GetParameterCount(_parameters))) {
                            try {
                                bool dataReady;
                                Debug.Assert(_stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
                                bool result = _stateObj.Parser.TryRun(RunBehavior.UntilDone, this, null, null, _stateObj, out dataReady);
                                if (!result) { throw SQL.SynchronousCallMayNotPend(); }
                            }
                            finally {
                                cachedAsyncState.ResetAsyncState();
                            }
                        }
                        else { // otherwise, use a full-fledged execute that can handle params and stored procs
                            SqlDataReader reader = CompleteAsyncExecuteReader();
                            if (null != reader) {
                                reader.Close();
                            }
                        }
                    }
                    catch (SqlException e) {
                        sqlExceptionNumber = e.Number;
                        throw;
                    }
                    catch (Exception e) {
                        processFinallyBlock = ADP.IsCatchableExceptionType(e);
                        throw;
                    }
                    finally {
                        if (processFinallyBlock) {
                            PutStateObject();
                        }
                    }

                    Debug.Assert(null == _stateObj, "non-null state object in EndExecuteNonQuery");
                    success = true;
                    return _rowsAffected;
                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif //DEBUG
            }
            catch (System.OutOfMemoryException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.StackOverflowException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.Threading.ThreadAbortException e) {
                _activeConnection.Abort(e);
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                throw;
            }
            catch (Exception e) {
                if (cachedAsyncState != null) {
                    cachedAsyncState.ResetAsyncState();
                };
                if (ADP.IsCatchableExceptionType(e)) {
                    ReliablePutStateObject();
                };
                throw;
            }
            finally {
                SqlStatistics.StopTimer(statistics);
                WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous: false);
            }
        }

        private Task InternalExecuteNonQuery(TaskCompletionSource<object> completion, string methodName, bool sendToPipe, int timeout, bool asyncWrite = false) {
            bool async = (null != completion);

            SqlStatistics statistics = Statistics;
            _rowsAffected = -1;

            TdsParser bestEffortCleanupTarget = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                {
#endif //DEBUG
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
                    // @devnote: this function may throw for an invalid connection
                    // @devnote: returns false for empty command text
                    ValidateCommand(methodName, async);
                    CheckNotificationStateAndAutoEnlist(); // Only call after validate - requires non null connection!

                    Task task = null;

                    // only send over SQL Batch command if we are not a stored proc and have no parameters and not in batch RPC mode
                    if ( _activeConnection.IsContextConnection ) {
                        if (null != statistics) {
                            statistics.SafeIncrement(ref statistics._unpreparedExecs);
                        }

                        RunExecuteNonQuerySmi( sendToPipe );
                    }
                    else if (!BatchRPCMode && (System.Data.CommandType.Text == this.CommandType) && (0 == GetParameterCount(_parameters))) {
                        Debug.Assert( !sendToPipe, "trying to send non-context command to pipe" );
                        if (null != statistics) {
                            if (!this.IsDirty && this.IsPrepared) {
                                statistics.SafeIncrement(ref statistics._preparedExecs);
                            }
                            else {
                                statistics.SafeIncrement(ref statistics._unpreparedExecs);
                            }
                        }

                        task = RunExecuteNonQueryTds(methodName, async, timeout, asyncWrite);
                    }
                    else  { // otherwise, use a full-fledged execute that can handle params and stored procs
                        Debug.Assert( !sendToPipe, "trying to send non-context command to pipe" );
                        Bid.Trace("<sc.SqlCommand.ExecuteNonQuery|INFO> %d#, Command executed as RPC.\n", ObjectID);
                        SqlDataReader reader = RunExecuteReader(0, RunBehavior.UntilDone, false, methodName, completion, timeout, out task, asyncWrite);
                        if (null!=reader) {
                            if (task != null) {
                                task = AsyncHelper.CreateContinuationTask(task, () => reader.Close());
                            }
                            else {
                                reader.Close();
                            }
                        }
                    }
                    Debug.Assert(async || null == _stateObj, "non-null state object in InternalExecuteNonQuery");
                    return task;
                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif //DEBUG
            }
            catch (System.OutOfMemoryException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.StackOverflowException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.Threading.ThreadAbortException e)  {
                _activeConnection.Abort(e);
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                throw;
            }
        }

        public XmlReader ExecuteXmlReader() {
            SqlConnection.ExecutePermission.Demand();

            // Reset _pendingCancel upon entry into any Execute - used to synchronize state
            // between entry into Execute* API and the thread obtaining the stateObject.
            _pendingCancel = false;

            SqlStatistics statistics = null;
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<sc.SqlCommand.ExecuteXmlReader|API> %d#", ObjectID);
            Bid.CorrelationTrace("<sc.SqlCommand.ExecuteXmlReader|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            bool success = false;
            int? sqlExceptionNumber = null;
            try {
                statistics = SqlStatistics.StartTimer(Statistics);
                WriteBeginExecuteEvent();

                // use the reader to consume metadata
                SqlDataReader ds;
                ds = RunExecuteReader(CommandBehavior.SequentialAccess, RunBehavior.ReturnImmediately, true, ADP.ExecuteXmlReader);
                XmlReader result = CompleteXmlReader(ds);
                success = true;
                return result;
            }
            catch (SqlException ex) {
                sqlExceptionNumber = ex.Number;
                throw;
            }
            finally {
                SqlStatistics.StopTimer(statistics);
                Bid.ScopeLeave(ref hscp);
                WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous : true);
            }
        }

        [System.Security.Permissions.HostProtectionAttribute(ExternalThreading=true)]
        public IAsyncResult BeginExecuteXmlReader() {
            // BeginExecuteXmlReader will track executiontime
            return BeginExecuteXmlReader(null, null);
        }

        [System.Security.Permissions.HostProtectionAttribute(ExternalThreading=true)]
        public IAsyncResult BeginExecuteXmlReader(AsyncCallback callback, object stateObject) {
            Bid.CorrelationTrace("<sc.SqlCommand.BeginExecuteXmlReader|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            SqlConnection.ExecutePermission.Demand();   
            return BeginExecuteXmlReaderInternal(callback, stateObject, 0);
        }

        private IAsyncResult BeginExecuteXmlReaderAsync(AsyncCallback callback, object stateObject) {
            return BeginExecuteXmlReaderInternal(callback, stateObject, CommandTimeout, asyncWrite:true);
        }

        private IAsyncResult BeginExecuteXmlReaderInternal(AsyncCallback callback, object stateObject, int timeout, bool asyncWrite = false) {        
            // Reset _pendingCancel upon entry into any Execute - used to synchronize state
            // between entry into Execute* API and the thread obtaining the stateObject.
            _pendingCancel = false;

            ValidateAsyncCommand(); // Special case - done outside of try/catches to prevent putting a stateObj
                                    // back into pool when we should not.

            SqlStatistics statistics = null;
            try {
                statistics = SqlStatistics.StartTimer(Statistics);
                WriteBeginExecuteEvent();
                TaskCompletionSource<object> completion = new TaskCompletionSource<object>(stateObject);

                Task writeTask;
                try { // InternalExecuteNonQuery already has reliability block, but if failure will not put stateObj back into pool.
                    RunExecuteReader(CommandBehavior.SequentialAccess, RunBehavior.ReturnImmediately, true, ADP.BeginExecuteXmlReader, completion, timeout, out writeTask, asyncWrite);
                }
                catch (Exception e) {
                    if (!ADP.IsCatchableOrSecurityExceptionType(e)) {
                        // If not catchable - the connection has already been caught and doomed in RunExecuteReader.
                        throw;
                    }
        
                    // For async, RunExecuteReader will never put the stateObj back into the pool, so do so now.
                    ReliablePutStateObject(); 
                    throw;
                }

                if (writeTask != null) {
                    AsyncHelper.ContinueTask(writeTask, completion, () => BeginExecuteXmlReaderInternalReadStage(completion));
                }
                else {
                    BeginExecuteXmlReaderInternalReadStage(completion);
                }

                // Add callback after work is done to avoid overlapping Begin\End methods
                if (callback != null) {
                    completion.Task.ContinueWith((t) => callback(t), TaskScheduler.Default);
                }
                return completion.Task;
            }
            finally {
                SqlStatistics.StopTimer(statistics);
            }
        }

        private void BeginExecuteXmlReaderInternalReadStage(TaskCompletionSource<object> completion) {
            Debug.Assert(completion != null,"Completion source should not be null");
            // Read SNI does not have catches for async exceptions, handle here.
            TdsParser bestEffortCleanupTarget = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                    {
#endif //DEBUG
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
                    // must finish caching information before ReadSni which can activate the callback before returning
                    cachedAsyncState.SetActiveConnectionAndResult(completion, ADP.EndExecuteXmlReader, _activeConnection);
                    _stateObj.ReadSni(completion);
                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif //DEBUG
            }
            catch (System.OutOfMemoryException e) {
                _activeConnection.Abort(e);
                completion.TrySetException(e);
                throw;
            }
            catch (System.StackOverflowException e) {
                _activeConnection.Abort(e);
                completion.TrySetException(e);
                throw;
            }
            catch (System.Threading.ThreadAbortException e) {
                _activeConnection.Abort(e);
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                completion.TrySetException(e);
                throw;
            }
            catch (Exception e) {
                // Similarly, if an exception occurs put the stateObj back into the pool.
                // and reset async cache information to allow a second async execute
                if (null != _cachedAsyncState) {
                    _cachedAsyncState.ResetAsyncState();
                }
                ReliablePutStateObject();
                completion.TrySetException(e);
            }
        }

        public XmlReader EndExecuteXmlReader(IAsyncResult asyncResult) {
            try {
                return EndExecuteXmlReaderInternal(asyncResult);
            }
            finally {
                Bid.CorrelationTrace("<sc.SqlCommand.EndExecuteXmlReader|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            }
        }

        private XmlReader EndExecuteXmlReaderAsync(IAsyncResult asyncResult) {
            Bid.CorrelationTrace("<sc.SqlCommand.EndExecuteXmlReaderAsync|Info|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);

            Exception asyncException = ((Task)asyncResult).Exception;
            if (asyncException != null) {
                // Leftover exception from the Begin...InternalReadStage
                ReliablePutStateObject();
                throw asyncException.InnerException;
            }
            else {
                ThrowIfReconnectionHasBeenCanceled();
                // lock on _stateObj prevents ----s with close/cancel.
                lock (_stateObj) {
                    return EndExecuteXmlReaderInternal(asyncResult);
                }
            }
        }

        private XmlReader EndExecuteXmlReaderInternal(IAsyncResult asyncResult) {
            bool success = false;
            int? sqlExceptionNumber = null;
            try {
                XmlReader result = CompleteXmlReader(InternalEndExecuteReader(asyncResult, ADP.EndExecuteXmlReader));
                success = true;
                return result;
            }
            catch (SqlException e){
                sqlExceptionNumber = e.Number;
                if (cachedAsyncState != null) {
                    cachedAsyncState.ResetAsyncState();
                };

                //  SqlException is always catchable 
                ReliablePutStateObject();
                throw;
            }
            catch (Exception e) {
                if (cachedAsyncState != null) {
                    cachedAsyncState.ResetAsyncState();
                };
                if (ADP.IsCatchableExceptionType(e)) {
                    ReliablePutStateObject();
                };
                throw;
            }
            finally {
                WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous : false);
            }
        }

        private XmlReader CompleteXmlReader(SqlDataReader ds) {
            XmlReader xr = null;

            SmiExtendedMetaData[] md = ds.GetInternalSmiMetaData();
            bool isXmlCapable = (null != md && md.Length == 1 && (md[0].SqlDbType == SqlDbType.NText 
                                                         || md[0].SqlDbType == SqlDbType.NVarChar 
                                                         || md[0].SqlDbType == SqlDbType.Xml));

            if (isXmlCapable) {
                try {
                    SqlStream sqlBuf = new SqlStream(ds, true /*addByteOrderMark*/, (md[0].SqlDbType == SqlDbType.Xml) ? false : true /*process all rows*/);
                    xr = sqlBuf.ToXmlReader();
                }
                catch (Exception e) {
                    if (ADP.IsCatchableExceptionType(e)) {
                        ds.Close();
                    }
                    throw;
                }
            }
            if (xr == null) {
                ds.Close();
                throw SQL.NonXmlResult();
            }
            return xr;
        }

        [System.Security.Permissions.HostProtectionAttribute(ExternalThreading=true)]
        public IAsyncResult BeginExecuteReader() {
            return BeginExecuteReader(null, null, CommandBehavior.Default);
        }

        [System.Security.Permissions.HostProtectionAttribute(ExternalThreading=true)]
        public IAsyncResult BeginExecuteReader(AsyncCallback callback, object stateObject) {
            return BeginExecuteReader(callback, stateObject, CommandBehavior.Default);
        }

        override protected DbDataReader ExecuteDbDataReader(CommandBehavior behavior) {
            Bid.CorrelationTrace("<sc.SqlCommand.ExecuteDbDataReader|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            return ExecuteReader(behavior, ADP.ExecuteReader);
        }

        new public SqlDataReader ExecuteReader() {
            SqlStatistics statistics = null;
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<sc.SqlCommand.ExecuteReader|API> %d#", ObjectID);
            Bid.CorrelationTrace("<sc.SqlCommand.ExecuteReader|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            try {
                statistics = SqlStatistics.StartTimer(Statistics);
                return ExecuteReader(CommandBehavior.Default, ADP.ExecuteReader);
            }
            finally {
                SqlStatistics.StopTimer(statistics);
                Bid.ScopeLeave(ref hscp);
            }
        }

        new public SqlDataReader ExecuteReader(CommandBehavior behavior) {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<sc.SqlCommand.ExecuteReader|API> %d#, behavior=%d{ds.CommandBehavior}", ObjectID, (int)behavior);
            Bid.CorrelationTrace("<sc.SqlCommand.ExecuteReader|API|Correlation> ObjectID%d#, behavior=%d{ds.CommandBehavior}, ActivityID %ls\n", ObjectID, (int)behavior);
            try {
                return ExecuteReader(behavior, ADP.ExecuteReader);
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }

        [System.Security.Permissions.HostProtectionAttribute(ExternalThreading=true)]
        public IAsyncResult BeginExecuteReader(CommandBehavior behavior) {
            return BeginExecuteReader(null, null, behavior);
        }

        [System.Security.Permissions.HostProtectionAttribute(ExternalThreading=true)]
        public IAsyncResult BeginExecuteReader(AsyncCallback callback, object stateObject, CommandBehavior behavior) {
            Bid.CorrelationTrace("<sc.SqlCommand.BeginExecuteReader|API|Correlation> ObjectID%d#, behavior=%d{ds.CommandBehavior}, ActivityID %ls\n", ObjectID, (int)behavior);
            SqlConnection.ExecutePermission.Demand();
            return BeginExecuteReaderInternal(behavior, callback, stateObject, 0);
        }

        internal SqlDataReader ExecuteReader(CommandBehavior behavior, string method) {
            SqlConnection.ExecutePermission.Demand(); // 

            // Reset _pendingCancel upon entry into any Execute - used to synchronize state
            // between entry into Execute* API and the thread obtaining the stateObject.
            _pendingCancel = false;

            SqlStatistics statistics = null;

            TdsParser bestEffortCleanupTarget = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            bool success = false;
            int? sqlExceptionNumber = null;
            try {
                WriteBeginExecuteEvent();
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                {
#endif //DEBUG
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
                    statistics = SqlStatistics.StartTimer(Statistics);
                    SqlDataReader result = RunExecuteReader(behavior, RunBehavior.ReturnImmediately, true, method);
                    success = true;
                    return result;
                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif //DEBUG
            }
            catch (SqlException e) {
                sqlExceptionNumber = e.Number;
                throw;
            }
            catch (System.OutOfMemoryException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.StackOverflowException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.Threading.ThreadAbortException e)  {
                _activeConnection.Abort(e);
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                throw;
            }
            finally {
                SqlStatistics.StopTimer(statistics);
                WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous : true);
            }
        }

        public SqlDataReader EndExecuteReader(IAsyncResult asyncResult) {
            try {
                return EndExecuteReaderInternal(asyncResult);
            }          
            finally {
                Bid.CorrelationTrace("<sc.SqlCommand.EndExecuteReader|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            }
        }

        private SqlDataReader EndExecuteReaderAsync(IAsyncResult asyncResult) {
            Bid.CorrelationTrace("<sc.SqlCommand.EndExecuteReaderAsync|Info|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);

            Exception asyncException = ((Task)asyncResult).Exception;
            if (asyncException != null) {
                // Leftover exception from the Begin...InternalReadStage
                ReliablePutStateObject();
                throw asyncException.InnerException;
            }
            else {
                ThrowIfReconnectionHasBeenCanceled();
                // lock on _stateObj prevents ----s with close/cancel.
                lock (_stateObj) {
                        return EndExecuteReaderInternal(asyncResult);
                }
            }
        }

        private SqlDataReader EndExecuteReaderInternal(IAsyncResult asyncResult) {
            SqlStatistics statistics = null;
            bool success = false;
            int? sqlExceptionNumber = null;
            try {
                statistics = SqlStatistics.StartTimer(Statistics);
                SqlDataReader result = InternalEndExecuteReader(asyncResult, ADP.EndExecuteReader);
                success = true;
                return result;
            }
            catch (SqlException e) {
                sqlExceptionNumber = e.Number;
                if (cachedAsyncState != null)
                {
                    cachedAsyncState.ResetAsyncState();
                };

                //  SqlException is always catchable 
                ReliablePutStateObject();
                throw;
            }
            catch (Exception e) {
                if (cachedAsyncState != null) {
                    cachedAsyncState.ResetAsyncState();
                };
                if (ADP.IsCatchableExceptionType(e)) {
                    ReliablePutStateObject();
                };
                throw;
            }
            finally {
                SqlStatistics.StopTimer(statistics);
                WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous : false);
            }
        }

        private IAsyncResult BeginExecuteReaderAsync(CommandBehavior behavior, AsyncCallback callback, object stateObject) {
            return BeginExecuteReaderInternal(behavior, callback, stateObject, CommandTimeout, asyncWrite:true);
        }

        private IAsyncResult BeginExecuteReaderInternal(CommandBehavior behavior, AsyncCallback callback, object stateObject, int timeout, bool asyncWrite = false) {        
            // Reset _pendingCancel upon entry into any Execute - used to synchronize state
            // between entry into Execute* API and the thread obtaining the stateObject.
            _pendingCancel = false;

            SqlStatistics statistics = null;
            try {
                statistics = SqlStatistics.StartTimer(Statistics);
                WriteBeginExecuteEvent();
                TaskCompletionSource<object> completion = new TaskCompletionSource<object>(stateObject);

                ValidateAsyncCommand(); // Special case - done outside of try/catches to prevent putting a stateObj
                                        // back into pool when we should not.

                Task writeTask = null;
                try { // InternalExecuteNonQuery already has reliability block, but if failure will not put stateObj back into pool.
                    RunExecuteReader(behavior, RunBehavior.ReturnImmediately, true, ADP.BeginExecuteReader, completion, timeout, out writeTask, asyncWrite);
                }
                catch (Exception e) {
                    if (!ADP.IsCatchableOrSecurityExceptionType(e)) {
                        // If not catchable - the connection has already been caught and doomed in RunExecuteReader.
                        throw;
                    }
    
                    // For async, RunExecuteReader will never put the stateObj back into the pool, so do so now.
                    ReliablePutStateObject(); 
                    throw;
                }

                if (writeTask != null ) {
                    AsyncHelper.ContinueTask(writeTask,completion,()=> BeginExecuteReaderInternalReadStage(completion));
                }
                else {
                    BeginExecuteReaderInternalReadStage(completion);
                }

                // Add callback after work is done to avoid overlapping Begin\End methods
                if (callback != null) {
                    completion.Task.ContinueWith((t) => callback(t), TaskScheduler.Default);
                }
                return completion.Task;
            }
            finally {
                SqlStatistics.StopTimer(statistics);
            }
        }

        private void BeginExecuteReaderInternalReadStage(TaskCompletionSource<object> completion) {
            Debug.Assert(completion != null,"CompletionSource should not be null");
            // Read SNI does not have catches for async exceptions, handle here.
            TdsParser bestEffortCleanupTarget = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                    {
#endif //DEBUG
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
                    // must finish caching information before ReadSni which can activate the callback before returning
                    cachedAsyncState.SetActiveConnectionAndResult(completion, ADP.EndExecuteReader, _activeConnection);
                    _stateObj.ReadSni(completion);
                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif //DEBUG
            }
            catch (System.OutOfMemoryException e) {
                _activeConnection.Abort(e);
                completion.TrySetException(e);
                throw;
            }
            catch (System.StackOverflowException e) {
                _activeConnection.Abort(e);
                completion.TrySetException(e);
                throw;
            }
            catch (System.Threading.ThreadAbortException e) {
                _activeConnection.Abort(e);
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                completion.TrySetException(e);
                throw;
            }
            catch (Exception e) {
                // Similarly, if an exception occurs put the stateObj back into the pool.
                // and reset async cache information to allow a second async execute
                if (null != _cachedAsyncState) {
                    _cachedAsyncState.ResetAsyncState();
                }
                ReliablePutStateObject();
                completion.TrySetException(e);
            }
        }

        private SqlDataReader InternalEndExecuteReader(IAsyncResult asyncResult, string endMethod) {

            VerifyEndExecuteState((Task) asyncResult, endMethod);
            WaitForAsyncResults(asyncResult);

            // If Transparent parameter encryption was attempted, then we would have skipped the below 
            // checks in VerifyEndExecuteState since we wanted to wait for WaitForAsyncResults to complete.
            if (IsColumnEncryptionEnabled) {
                if (cachedAsyncState.EndMethodName == null) {
                    throw ADP.MethodCalledTwice(endMethod);
                }

                if (endMethod != cachedAsyncState.EndMethodName) {
                    throw ADP.MismatchedAsyncResult(cachedAsyncState.EndMethodName, endMethod);
                }

                if (!cachedAsyncState.IsActiveConnectionValid(_activeConnection)) {
                    // If the connection is not 'valid' then it was closed while we were executing
                    throw ADP.ClosedConnectionError();
                }
            }

            CheckThrowSNIException();

            TdsParser bestEffortCleanupTarget = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                {
#endif //DEBUG
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
                    SqlDataReader reader = CompleteAsyncExecuteReader();
                    Debug.Assert(null == _stateObj, "non-null state object in InternalEndExecuteReader");
                    return reader;
                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif //DEBUG
            }
            catch (System.OutOfMemoryException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.StackOverflowException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.Threading.ThreadAbortException e)  {
                _activeConnection.Abort(e);
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                throw;
            }
        }

        public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken) {

            Bid.CorrelationTrace("<sc.SqlCommand.ExecuteNonQueryAsync|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            SqlConnection.ExecutePermission.Demand();   

            TaskCompletionSource<int> source = new TaskCompletionSource<int>();

            CancellationTokenRegistration registration = new CancellationTokenRegistration();
            if (cancellationToken.CanBeCanceled) {
                if (cancellationToken.IsCancellationRequested) {
                    source.SetCanceled();
                    return source.Task;
                }
                registration = cancellationToken.Register(CancelIgnoreFailure);
            }

            Task<int> returnedTask = source.Task;
            try {
                RegisterForConnectionCloseNotification(ref returnedTask);

                Task<int>.Factory.FromAsync(BeginExecuteNonQueryAsync, EndExecuteNonQueryAsync, null).ContinueWith((t) => {
                    registration.Dispose();
                    if (t.IsFaulted) {
                        Exception e = t.Exception.InnerException;
                        source.SetException(e);
                    }
                    else {
                        if (t.IsCanceled) {
                            source.SetCanceled();
                        }
                        else {
                            source.SetResult(t.Result);
                        }
                    }
                }, TaskScheduler.Default);
            } 
            catch (Exception e) {
                source.SetException(e);
            }

            return returnedTask;
        }

        protected override Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) {
            return ExecuteReaderAsync(behavior, cancellationToken).ContinueWith<DbDataReader>((result) => {
                if (result.IsFaulted) {
                    throw result.Exception.InnerException;
                }
                return result.Result;
            }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.NotOnCanceled, TaskScheduler.Default);
        }

        new public Task<SqlDataReader> ExecuteReaderAsync() {
            return ExecuteReaderAsync(CommandBehavior.Default, CancellationToken.None);
        }

        new public Task<SqlDataReader> ExecuteReaderAsync(CommandBehavior behavior) {
            return ExecuteReaderAsync(behavior, CancellationToken.None);
        }

        new public Task<SqlDataReader> ExecuteReaderAsync(CancellationToken cancellationToken) {
            return ExecuteReaderAsync(CommandBehavior.Default, cancellationToken);
        }

        new public Task<SqlDataReader> ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) {

            Bid.CorrelationTrace("<sc.SqlCommand.ExecuteReaderAsync|API|Correlation> ObjectID%d#, behavior=%d{ds.CommandBehavior}, ActivityID %ls\n", ObjectID, (int)behavior);
            SqlConnection.ExecutePermission.Demand();   

            TaskCompletionSource<SqlDataReader> source = new TaskCompletionSource<SqlDataReader>();

            CancellationTokenRegistration registration = new CancellationTokenRegistration();
            if (cancellationToken.CanBeCanceled) {
                if (cancellationToken.IsCancellationRequested) {
                    source.SetCanceled();
                    return source.Task;
                }
                registration = cancellationToken.Register(CancelIgnoreFailure);
            }
            
            Task<SqlDataReader> returnedTask = source.Task;
            try {
                RegisterForConnectionCloseNotification(ref returnedTask);

                Task<SqlDataReader>.Factory.FromAsync(BeginExecuteReaderAsync, EndExecuteReaderAsync, behavior, null).ContinueWith((t) => {                    
                    registration.Dispose();
                    if (t.IsFaulted) {
                        Exception e = t.Exception.InnerException;
                        source.SetException(e);
                    }
                    else {
                        if (t.IsCanceled) {
                            source.SetCanceled();
                        }
                        else {
                            source.SetResult(t.Result);
                        }
                    }
                }, TaskScheduler.Default);
            } 
            catch (Exception e) {
                source.SetException(e);
            }

            return returnedTask;
        }

        public override Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
        {
            return ExecuteReaderAsync(cancellationToken).ContinueWith((executeTask) => {
                TaskCompletionSource<object> source = new TaskCompletionSource<object>();
                if (executeTask.IsCanceled) {
                    source.SetCanceled();
                }
                else if (executeTask.IsFaulted) {
                    source.SetException(executeTask.Exception.InnerException);
                }
                else {
                    SqlDataReader reader = executeTask.Result;
                    reader.ReadAsync(cancellationToken).ContinueWith((readTask) => {
                        try {
                            if (readTask.IsCanceled) {
                                reader.Dispose();
                                source.SetCanceled();
                            }
                            else if (readTask.IsFaulted) {
                                reader.Dispose();
                                source.SetException(readTask.Exception.InnerException);
                            } 
                            else {
                                Exception exception = null;
                                object result = null;
                                try {
                                    bool more = readTask.Result;
                                    if (more && reader.FieldCount > 0) {
                                        try {
                                            result = reader.GetValue(0);
                                        } 
                                        catch (Exception e) {
                                            exception = e;
                                        }
                                    }
                                }
                                finally {
                                    reader.Dispose();
                                }
                                if (exception != null) {
                                    source.SetException(exception);
                                }
                                else {
                                    source.SetResult(result);
                                }
                            }
                        }
                        catch (Exception e) {
                            // exception thrown by Dispose...
                            source.SetException(e);
                        }
                    }, TaskScheduler.Default);
                }
                return source.Task;
            }, TaskScheduler.Default).Unwrap();
        }

        public Task<XmlReader> ExecuteXmlReaderAsync() {
            return ExecuteXmlReaderAsync(CancellationToken.None);
        }

        public Task<XmlReader> ExecuteXmlReaderAsync(CancellationToken cancellationToken) {

            Bid.CorrelationTrace("<sc.SqlCommand.ExecuteXmlReaderAsync|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
            SqlConnection.ExecutePermission.Demand();   
            
            TaskCompletionSource<XmlReader> source = new TaskCompletionSource<XmlReader>();

            CancellationTokenRegistration registration = new CancellationTokenRegistration();
            if (cancellationToken.CanBeCanceled) {
                if (cancellationToken.IsCancellationRequested) {
                    source.SetCanceled();
                    return source.Task;
                }
                registration = cancellationToken.Register(CancelIgnoreFailure);
            }
            
            Task<XmlReader> returnedTask = source.Task;
            try {
                RegisterForConnectionCloseNotification(ref returnedTask);

                Task<XmlReader>.Factory.FromAsync(BeginExecuteXmlReaderAsync, EndExecuteXmlReaderAsync, null).ContinueWith((t) => {
                    registration.Dispose();
                    if (t.IsFaulted) {
                        Exception e = t.Exception.InnerException;
                        source.SetException(e);
                    }
                    else {
                        if (t.IsCanceled) {
                            source.SetCanceled();
                        }
                        else {
                            source.SetResult(t.Result);
                        }
                    }
                }, TaskScheduler.Default);
            } 
            catch (Exception e) {
                source.SetException(e);
            }

            return returnedTask;
        }

        // If the user part is quoted, remove first and last brackets and then unquote any right square
        // brackets in the procedure.  This is a very simple parser that performs no validation.  As
        // with the function below, ideally we should have support from the server for this.
        private static string UnquoteProcedurePart(string part) {
            if ((null != part) && (2 <= part.Length)) {
                if ('[' == part[0] && ']' == part[part.Length-1]) {
                    part = part.Substring(1, part.Length-2); // strip outer '[' & ']'
                    part = part.Replace("]]", "]"); // undo quoted "]" from "]]" to "]"
                }
            }
            return part;
        }

        // User value in this format: [server].[database].[schema].[sp_foo];1
        // This function should only be passed "[sp_foo];1".
        // This function uses a pretty simple parser that doesn't do any validation.
        // Ideally, we would have support from the server rather than us having to do this.
        private static string UnquoteProcedureName(string name, out object groupNumber) {
            groupNumber  = null; // Out param - initialize value to no value.
            string sproc = name;

            if (null != sproc) {
                if (Char.IsDigit(sproc[sproc.Length-1])) { // If last char is a digit, parse.
                    int semicolon = sproc.LastIndexOf(';');
                    if (semicolon != -1) { // If we found a semicolon, obtain the integer.
                        string part   = sproc.Substring(semicolon+1);
                        int    number = 0;
                        if (Int32.TryParse(part, out number)) { // No checking, just fail if this doesn't work.
                            groupNumber = number;
                            sproc = sproc.Substring(0, semicolon);
                        }
                    }
                }
                sproc = UnquoteProcedurePart(sproc);
            }
            return sproc;
        }

        //index into indirection arrays for columns of interest to DeriveParameters
        private enum ProcParamsColIndex {
            ParameterName = 0,
            ParameterType,
            DataType,                  // obsolete in katmai, use ManagedDataType instead
            ManagedDataType,          // new in katmai
            CharacterMaximumLength,
            NumericPrecision,
            NumericScale,
            TypeCatalogName,
            TypeSchemaName,
            TypeName,
            XmlSchemaCollectionCatalogName,
            XmlSchemaCollectionSchemaName,
            XmlSchemaCollectionName,
            UdtTypeName,                // obsolete in Katmai.  Holds the actual typename if UDT, since TypeName didn't back then.
            DateTimeScale               // new in Katmai
        };

        // Yukon- column ordinals (this array indexed by ProcParamsColIndex
        static readonly internal string[] PreKatmaiProcParamsNames = new string[] {
            "PARAMETER_NAME",           // ParameterName,
            "PARAMETER_TYPE",           // ParameterType,
            "DATA_TYPE",                // DataType
            null,                       // ManagedDataType,     introduced in Katmai
            "CHARACTER_MAXIMUM_LENGTH", // CharacterMaximumLength,
            "NUMERIC_PRECISION",        // NumericPrecision,
            "NUMERIC_SCALE",            // NumericScale,
            "UDT_CATALOG",              // TypeCatalogName,
            "UDT_SCHEMA",               // TypeSchemaName,
            "TYPE_NAME",                // TypeName,
            "XML_CATALOGNAME",          // XmlSchemaCollectionCatalogName,
            "XML_SCHEMANAME",           // XmlSchemaCollectionSchemaName,
            "XML_SCHEMACOLLECTIONNAME", // XmlSchemaCollectionName
            "UDT_NAME",                 // UdtTypeName
            null,                       // Scale for datetime types with scale, introduced in Katmai
        };

        // Katmai+ column ordinals (this array indexed by ProcParamsColIndex
        static readonly internal string[] KatmaiProcParamsNames = new string[] {
            "PARAMETER_NAME",           // ParameterName,
            "PARAMETER_TYPE",           // ParameterType,
            null,                       // DataType, removed from Katmai+
            "MANAGED_DATA_TYPE",        // ManagedDataType,
            "CHARACTER_MAXIMUM_LENGTH", // CharacterMaximumLength,
            "NUMERIC_PRECISION",        // NumericPrecision,
            "NUMERIC_SCALE",            // NumericScale,
            "TYPE_CATALOG_NAME",        // TypeCatalogName,
            "TYPE_SCHEMA_NAME",         // TypeSchemaName,
            "TYPE_NAME",                // TypeName,
            "XML_CATALOGNAME",          // XmlSchemaCollectionCatalogName,
            "XML_SCHEMANAME",           // XmlSchemaCollectionSchemaName,
            "XML_SCHEMACOLLECTIONNAME", // XmlSchemaCollectionName
            null,                       // UdtTypeName, removed from Katmai+
            "SS_DATETIME_PRECISION",    // Scale for datetime types with scale
        };


        internal void DeriveParameters() {
            switch (this.CommandType) {
                case System.Data.CommandType.Text:
                    throw ADP.DeriveParametersNotSupported(this);
                case System.Data.CommandType.StoredProcedure:
                    break;
                case System.Data.CommandType.TableDirect:
                    // CommandType.TableDirect - do nothing, parameters are not supported
                    throw ADP.DeriveParametersNotSupported(this);
                default:
                    throw ADP.InvalidCommandType(this.CommandType);
            }

            // validate that we have a valid connection
            ValidateCommand(ADP.DeriveParameters, false /*not async*/);

            // Use common parser for SqlClient and OleDb - parse into 4 parts - Server, Catalog, Schema, ProcedureName
            string[] parsedSProc = MultipartIdentifier.ParseMultipartIdentifier(this.CommandText, "[\"", "]\"", Res.SQL_SqlCommandCommandText, false);
            if (null == parsedSProc[3] || ADP.IsEmpty(parsedSProc[3]))
            {
                throw ADP.NoStoredProcedureExists(this.CommandText);
            }

            Debug.Assert(parsedSProc.Length == 4, "Invalid array length result from SqlCommandBuilder.ParseProcedureName");

            SqlCommand    paramsCmd = null;
            StringBuilder cmdText   = new StringBuilder();

            // Build call for sp_procedure_params_rowset built of unquoted values from user:
            // [user server, if provided].[user catalog, else current database].[sys if Yukon, else blank].[sp_procedure_params_rowset]

            // Server - pass only if user provided.
            if (!ADP.IsEmpty(parsedSProc[0])) {
                SqlCommandSet.BuildStoredProcedureName(cmdText, parsedSProc[0]);
                cmdText.Append(".");
            }

            // Catalog - pass user provided, otherwise use current database.
            if (ADP.IsEmpty(parsedSProc[1])) {
                parsedSProc[1] = this.Connection.Database;
            }
            SqlCommandSet.BuildStoredProcedureName(cmdText, parsedSProc[1]);
            cmdText.Append(".");

            // Schema - only if Yukon, and then only pass sys.  Also - pass managed version of sproc
            // for Yukon, else older sproc.
            string[] colNames;
            bool useManagedDataType;
            if (this.Connection.IsKatmaiOrNewer) {
                // Procedure - [sp_procedure_params_managed]
                cmdText.Append("[sys].[").Append(TdsEnums.SP_PARAMS_MGD10).Append("]");

                colNames = KatmaiProcParamsNames;
                useManagedDataType = true;
            }
            else {
                if (this.Connection.IsYukonOrNewer) {
                    // Procedure - [sp_procedure_params_managed]
                    cmdText.Append("[sys].[").Append(TdsEnums.SP_PARAMS_MANAGED).Append("]");
                }
                else {
                    // Procedure - [sp_procedure_params_rowset]
                    cmdText.Append(".[").Append(TdsEnums.SP_PARAMS).Append("]");
                }

                colNames = PreKatmaiProcParamsNames;
                useManagedDataType = false;
            }


            paramsCmd = new SqlCommand(cmdText.ToString(), this.Connection, this.Transaction);
            paramsCmd.CommandType = CommandType.StoredProcedure;

            object groupNumber;

            // Prepare parameters for sp_procedure_params_rowset:
            // 1) procedure name - unquote user value
            // 2) group number - parsed at the time we unquoted procedure name
            // 3) procedure schema - unquote user value

            // 



            paramsCmd.Parameters.Add(new SqlParameter("@procedure_name", SqlDbType.NVarChar, 255));
            paramsCmd.Parameters[0].Value = UnquoteProcedureName(parsedSProc[3], out groupNumber); // ProcedureName is 4rd element in parsed array

            if (null != groupNumber) {
                SqlParameter param = paramsCmd.Parameters.Add(new SqlParameter("@group_number", SqlDbType.Int));
                param.Value = groupNumber;
            }

            if (!ADP.IsEmpty(parsedSProc[2])) { // SchemaName is 3rd element in parsed array
                SqlParameter param = paramsCmd.Parameters.Add(new SqlParameter("@procedure_schema", SqlDbType.NVarChar, 255));
                param.Value = UnquoteProcedurePart(parsedSProc[2]);
            }

            SqlDataReader r = null;

            List<SqlParameter> parameters = new List<SqlParameter>();
            bool processFinallyBlock = true;
            
            try {
                r = paramsCmd.ExecuteReader();

                SqlParameter p = null;

                while (r.Read()) {
                    // each row corresponds to a parameter of the stored proc.  Fill in all the info
            
                    p = new SqlParameter();

                    // name
                    p.ParameterName = (string) r[colNames[(int)ProcParamsColIndex.ParameterName]];

                    // type
                    if (useManagedDataType) {
                        p.SqlDbType = (SqlDbType)(short)r[colNames[(int)ProcParamsColIndex.ManagedDataType]];

                        // Yukon didn't have as accurate of information as we're getting for Katmai, so re-map a couple of
                        //  types for backward compatability.
                        switch (p.SqlDbType) {
                            case SqlDbType.Image:
                            case SqlDbType.Timestamp:
                                p.SqlDbType = SqlDbType.VarBinary;
                                break;

                            case SqlDbType.NText:
                                p.SqlDbType = SqlDbType.NVarChar;
                                break;

                            case SqlDbType.Text:
                                p.SqlDbType = SqlDbType.VarChar;
                                break;

                            default:
                                break;
                        }
                    }
                    else {
                        p.SqlDbType = MetaType.GetSqlDbTypeFromOleDbType((short)r[colNames[(int)ProcParamsColIndex.DataType]], 
                            ADP.IsNull(r[colNames[(int)ProcParamsColIndex.TypeName]]) ? 
                                ADP.StrEmpty : 
                                (string)r[colNames[(int)ProcParamsColIndex.TypeName]]);
                    }

                    // size
                    object a = r[colNames[(int)ProcParamsColIndex.CharacterMaximumLength]];
                    if (a is int) {
                        int size = (int)a;

                        // Map MAX sizes correctly.  The Katmai server-side proc sends 0 for these instead of -1.
                        //  Should be fixed on the Katmai side, but would likely hold up the RI, and is safer to fix here.
                        //  If we can get the server-side fixed before shipping Katmai, we can remove this mapping.
                        if (0 == size && 
                                (p.SqlDbType == SqlDbType.NVarChar ||
                                 p.SqlDbType == SqlDbType.VarBinary ||
                                 p.SqlDbType == SqlDbType.VarChar)) {
                            size = -1;
                        }
                        p.Size = size;
                    }

                    // direction
                    p.Direction = ParameterDirectionFromOleDbDirection((short)r[colNames[(int)ProcParamsColIndex.ParameterType]]);

                    if (p.SqlDbType == SqlDbType.Decimal) {
                        p.ScaleInternal = (byte) ((short)r[colNames[(int)ProcParamsColIndex.NumericScale]] & 0xff);
                        p.PrecisionInternal = (byte)((short)r[colNames[(int)ProcParamsColIndex.NumericPrecision]] & 0xff);
                    }

                    // type name for Udt 
                    if (SqlDbType.Udt == p.SqlDbType) {

                        Debug.Assert(this._activeConnection.IsYukonOrNewer,"Invalid datatype token received from pre-yukon server");

                        string udtTypeName;
                        if (useManagedDataType) {
                            udtTypeName = (string)r[colNames[(int)ProcParamsColIndex.TypeName]];
                        }
                        else {
                            udtTypeName = (string)r[colNames[(int)ProcParamsColIndex.UdtTypeName]];
                        }

                        //read the type name
                        p.UdtTypeName = r[colNames[(int)ProcParamsColIndex.TypeCatalogName]]+"."+
                            r[colNames[(int)ProcParamsColIndex.TypeSchemaName]]+"."+
                            udtTypeName;
                    }

                    // type name for Structured types (same as for Udt's except assign p.TypeName instead of p.UdtTypeName
                    if (SqlDbType.Structured == p.SqlDbType) {

                        Debug.Assert(this._activeConnection.IsKatmaiOrNewer,"Invalid datatype token received from pre-katmai server");

                        //read the type name
                        p.TypeName = r[colNames[(int)ProcParamsColIndex.TypeCatalogName]]+"."+
                            r[colNames[(int)ProcParamsColIndex.TypeSchemaName]]+"."+
                            r[colNames[(int)ProcParamsColIndex.TypeName]];
                    }

                    // XmlSchema name for Xml types
                    if (SqlDbType.Xml == p.SqlDbType) {
                        object value;

                        value = r[colNames[(int)ProcParamsColIndex.XmlSchemaCollectionCatalogName]];
                        p.XmlSchemaCollectionDatabase = ADP.IsNull(value) ? String.Empty : (string) value;

                        value = r[colNames[(int)ProcParamsColIndex.XmlSchemaCollectionSchemaName]];
                        p.XmlSchemaCollectionOwningSchema = ADP.IsNull(value) ? String.Empty : (string) value;

                        value = r[colNames[(int)ProcParamsColIndex.XmlSchemaCollectionName]];
                        p.XmlSchemaCollectionName = ADP.IsNull(value) ? String.Empty : (string) value;
                    }

                    if (MetaType._IsVarTime(p.SqlDbType)) {
                        object value = r[colNames[(int)ProcParamsColIndex.DateTimeScale]];
                        if (value is int) {
                            p.ScaleInternal = (byte)(((int)value) & 0xff);
                        }
                    }

                    parameters.Add(p);
                }
            }
            catch (Exception e) {
                processFinallyBlock = ADP.IsCatchableExceptionType(e);
                throw;
            }
            finally {
                TdsParser.ReliabilitySection.Assert("unreliable call to DeriveParameters");  // you need to setup for a thread abort somewhere before you call this method
                if (processFinallyBlock) {
                    if (null != r)
                        r.Close();

                    // always unhook the user's connection
                    paramsCmd.Connection = null;
                }
            }

            if (parameters.Count == 0) {
                throw ADP.NoStoredProcedureExists(this.CommandText);
            }

            this.Parameters.Clear();

            foreach (SqlParameter temp in parameters) {
                this._parameters.Add(temp);
            }
        }

        private ParameterDirection ParameterDirectionFromOleDbDirection(short oledbDirection) {
            Debug.Assert(oledbDirection >= 1 && oledbDirection <= 4, "invalid parameter direction from params_rowset!");

            switch (oledbDirection) {
                case 2:
                    return ParameterDirection.InputOutput;
                case 3:
                    return ParameterDirection.Output;
                case 4:
                    return ParameterDirection.ReturnValue;
                default:
                    return ParameterDirection.Input;
            }

        }

        // get cached metadata
        internal _SqlMetaDataSet MetaData {
            get {
                return _cachedMetaData;
            }
        }

        // Check to see if notificactions auto enlistment is turned on. Enlist if so.
        private void CheckNotificationStateAndAutoEnlist() {
            // First, if auto-enlist is on, check server version and then obtain context if
            // present.  If so, auto enlist to the dependency ID given in the context data.
            if (NotificationAutoEnlist) {
                if (_activeConnection.IsYukonOrNewer) { // Only supported for Yukon...
                    string notifyContext = SqlNotificationContext();
                    if (!ADP.IsEmpty(notifyContext)) {
                        // Map to dependency by ID set in context data.
                        SqlDependency dependency = SqlDependencyPerAppDomainDispatcher.SingletonInstance.LookupDependencyEntry(notifyContext);

                        if (null != dependency) {
                            // Add this command to the dependency.
                            dependency.AddCommandDependency(this);
                        }
                    }
                }
            }

            // If we have a notification with a dependency, setup the notification options at this time.

            // If user passes options, then we will always have option data at the time the SqlDependency
            // ctor is called.  But, if we are using default queue, then we do not have this data until
            // Start().  Due to this, we always delay setting options until execute.

            // There is a variance in order between Start(), SqlDependency(), and Execute.  This is the 
            // best way to solve that problem.
            if (null != Notification) {
                if (_sqlDep != null) {
                    if (null == _sqlDep.Options) { 
                        // If null, SqlDependency was not created with options, so we need to obtain default options now.
                        // GetDefaultOptions can and will throw under certain conditions.

                        // In order to match to the appropriate start - we need 3 pieces of info:
                        // 1) server 2) user identity (SQL Auth or Int Sec) 3) database

                        SqlDependency.IdentityUserNamePair identityUserName = null;

                        // Obtain identity from connection.
                        SqlInternalConnectionTds internalConnection = _activeConnection.InnerConnection as SqlInternalConnectionTds;
                        if (internalConnection.Identity != null) {
                            identityUserName = new SqlDependency.IdentityUserNamePair(internalConnection.Identity, null);
                        }
                        else {
                            identityUserName = new SqlDependency.IdentityUserNamePair(null, internalConnection.ConnectionOptions.UserID);
                        }

                        Notification.Options = SqlDependency.GetDefaultComposedOptions(_activeConnection.DataSource,
                                                             InternalTdsConnection.ServerProvidedFailOverPartner,
                                                             identityUserName, _activeConnection.Database);
                    }

                    // Set UserData on notifications, as well as adding to the appdomain dispatcher.  The value is
                    // computed by an algorithm on the dependency - fixed and will always produce the same value
                    // given identical commandtext + parameter values.
                    Notification.UserData = _sqlDep.ComputeHashAndAddToDispatcher(this);
                    // Maintain server list for SqlDependency.
                    _sqlDep.AddToServerList(_activeConnection.DataSource);
                }
            }   
        }

        [System.Security.Permissions.SecurityPermission(SecurityAction.Assert, Infrastructure=true)]
        static internal string SqlNotificationContext() {
            SqlConnection.VerifyExecutePermission();

            // since this information is protected, follow it so that it is not exposed to the user.
            // SQLBU 329633, SQLBU 329637
            return (System.Runtime.Remoting.Messaging.CallContext.GetData("MS.SqlDependencyCookie") as string);
        }

        // Tds-specific logic for ExecuteNonQuery run handling
        private Task RunExecuteNonQueryTds(string methodName, bool async, int timeout, bool asyncWrite ) {
            Debug.Assert(!asyncWrite || async, "AsyncWrite should be always accompanied by Async");
            bool processFinallyBlock = true;
            try {

                Task reconnectTask = _activeConnection.ValidateAndReconnect(null, timeout);

                if (reconnectTask != null) {
                    long reconnectionStart = ADP.TimerCurrent();
                    if (async) {
                        TaskCompletionSource<object> completion = new TaskCompletionSource<object>();
                        _activeConnection.RegisterWaitingForReconnect(completion.Task);
                        _reconnectionCompletionSource = completion;
                        CancellationTokenSource timeoutCTS = new CancellationTokenSource();
                        AsyncHelper.SetTimeoutException(completion, timeout, SQL.CR_ReconnectTimeout, timeoutCTS.Token);                        
                        AsyncHelper.ContinueTask(reconnectTask, completion,
                            () => {                               
                                if (completion.Task.IsCompleted) {
                                    return;
                                }                                
                                Interlocked.CompareExchange(ref _reconnectionCompletionSource, null, completion);
                                timeoutCTS.Cancel();
                                Task subTask = RunExecuteNonQueryTds(methodName, async, TdsParserStaticMethods.GetRemainingTimeout(timeout, reconnectionStart), asyncWrite);
                                if (subTask == null) {
                                    completion.SetResult(null);
                                }
                                else {
                                    AsyncHelper.ContinueTask(subTask, completion, () => completion.SetResult(null));
                                }
                            }, connectionToAbort: _activeConnection);
                        return completion.Task;
                    }
                    else {
                        AsyncHelper.WaitForCompletion(reconnectTask, timeout, () => { throw SQL.CR_ReconnectTimeout(); });
                        timeout = TdsParserStaticMethods.GetRemainingTimeout(timeout, reconnectionStart);
                    }
                }

                if (asyncWrite) {
                    _activeConnection.AddWeakReference(this, SqlReferenceCollection.CommandTag);
                }

                GetStateObject();

                // we just send over the raw text with no annotation
                // no parameters are sent over
                // no data reader is returned
                // use this overload for "batch SQL" tds token type
                Bid.Trace("<sc.SqlCommand.ExecuteNonQuery|INFO> %d#, Command executed as SQLBATCH.\n", ObjectID);
                Task executeTask = _stateObj.Parser.TdsExecuteSQLBatch(this.CommandText, timeout, this.Notification, _stateObj, sync: true);
                Debug.Assert(executeTask == null, "Shouldn't get a task when doing sync writes");

                NotifyDependency();
                if (async) {
                    _activeConnection.GetOpenTdsConnection(methodName).IncrementAsyncCount();
                }
                else {
                    bool dataReady;
                    Debug.Assert(_stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
                    bool result = _stateObj.Parser.TryRun(RunBehavior.UntilDone, this, null, null, _stateObj, out dataReady);
                    if (!result) { throw SQL.SynchronousCallMayNotPend(); }
                }
            }
            catch (Exception e) {
                processFinallyBlock = ADP.IsCatchableExceptionType(e);
                throw;
            }
            finally {
                TdsParser.ReliabilitySection.Assert("unreliable call to RunExecuteNonQueryTds");  // you need to setup for a thread abort somewhere before you call this method
                if (processFinallyBlock && !async) {
                    // When executing Async, we need to keep the _stateObj alive...
                    PutStateObject();
                }
            }
            return null;
        }

        // Smi-specific logic for ExecuteNonQuery
        private void RunExecuteNonQuerySmi( bool sendToPipe ) {
            SqlInternalConnectionSmi innerConnection = InternalSmiConnection;

            // Set it up, process all of the events, and we're done!
            SmiRequestExecutor requestExecutor = null;
            try {
                requestExecutor = SetUpSmiRequest(innerConnection);
                SmiExecuteType execType;
                if ( sendToPipe )
                    execType = SmiExecuteType.ToPipe;
                else
                    execType = SmiExecuteType.NonQuery;


                SmiEventStream eventStream = null;
                // Don't need a CER here because caller already has one that will doom the
                //  connection if it's a finally-skipping type of problem.
                bool processFinallyBlock = true;
                try {
                    long transactionId;
                    SysTx.Transaction transaction;
                    innerConnection.GetCurrentTransactionPair(out transactionId, out transaction);

                    if (Bid.AdvancedOn) {
                        Bid.Trace("<sc.SqlCommand.RunExecuteNonQuerySmi|ADV> %d#, innerConnection=%d#, transactionId=0x%I64x, cmdBehavior=%d.\n", ObjectID, innerConnection.ObjectID, transactionId, (int)CommandBehavior.Default);
                    }

                    if (SmiContextFactory.Instance.NegotiatedSmiVersion >= SmiContextFactory.KatmaiVersion) {
                        eventStream = requestExecutor.Execute(
                                                          innerConnection.SmiConnection,
                                                          transactionId,
                                                          transaction,
                                                          CommandBehavior.Default,
                                                          execType);
                    }
                    else {
                        eventStream = requestExecutor.Execute(
                                                          innerConnection.SmiConnection,
                                                          transactionId,
                                                          CommandBehavior.Default,
                                                          execType);
                    }

                    while ( eventStream.HasEvents ) {
                        eventStream.ProcessEvent( EventSink );
                    }
                }
                catch (Exception e) {
                    processFinallyBlock = ADP.IsCatchableExceptionType(e);
                    throw;
                }
                finally {
                    TdsParser.ReliabilitySection.Assert("unreliable call to RunExecuteNonQuerySmi");  // you need to setup for a thread abort somewhere before you call this method
                    if (null != eventStream && processFinallyBlock) {
                        eventStream.Close( EventSink );
                    }
                }

                EventSink.ProcessMessagesAndThrow();
            }
            finally {
                if (requestExecutor != null) {
                    requestExecutor.Close(EventSink);
                    EventSink.ProcessMessagesAndThrow(ignoreNonFatalMessages: true);
                }
            }
        }

        /// <summary>
        /// Resets the encryption related state of the command object and each of the parameters.
        /// BatchRPC doesn't need special handling to cleanup the state of each RPC object and its parameters since a new RPC object and 
        /// parameters are generated on every execution.
        /// </summary>
        private void ResetEncryptionState() {
            // First reset the command level state.
            ClearDescribeParameterEncryptionRequests();

            // Reset the state of each of the parameters.
            if (_parameters != null) {
                for (int i = 0; i < _parameters.Count; i++) {
                    _parameters[i].CipherMetadata = null;
                    _parameters[i].HasReceivedMetadata = false;
                }
            }
        }

        /// <summary>
        /// Steps to be executed in the Prepare Transparent Encryption finally block.
        /// </summary>
        private void PrepareTransparentEncryptionFinallyBlock(  bool closeDataReader,
                                                                bool clearDataStructures,
                                                                bool decrementAsyncCount,
                                                                bool wasDescribeParameterEncryptionNeeded,
                                                                ReadOnlyDictionary<_SqlRPC, _SqlRPC> describeParameterEncryptionRpcOriginalRpcMap,
                                                                SqlDataReader describeParameterEncryptionDataReader) {
            if (clearDataStructures) {
                // Clear some state variables in SqlCommand that reflect in-progress describe parameter encryption requests.
                ClearDescribeParameterEncryptionRequests();

                if (describeParameterEncryptionRpcOriginalRpcMap != null) {
                    describeParameterEncryptionRpcOriginalRpcMap = null;
                }
            }

            // Decrement the async count.
            if (decrementAsyncCount) {
                SqlInternalConnectionTds internalConnectionTds = _activeConnection.GetOpenTdsConnection();
                if (internalConnectionTds != null) {
                    internalConnectionTds.DecrementAsyncCount();
                }
            }

            if (closeDataReader) {
                // Close the data reader to reset the _stateObj
                if (null != describeParameterEncryptionDataReader) {
                    describeParameterEncryptionDataReader.Close();
                }
            }
        }

        /// <summary>
        /// Executes the reader after checking to see if we need to encrypt input parameters and then encrypting it if required.
        /// TryFetchInputParameterEncryptionInfo() -> ReadDescribeEncryptionParameterResults()-> EncryptInputParameters() ->RunExecuteReaderTds()
        /// </summary>
        /// <param name="cmdBehavior"></param>
        /// <param name="returnStream"></param>
        /// <param name="async"></param>
        /// <param name="timeout"></param>
        /// <param name="task"></param>
        /// <param name="asyncWrite"></param>
        /// <returns></returns>
        private void PrepareForTransparentEncryption(CommandBehavior cmdBehavior, bool returnStream, bool async, int timeout, TaskCompletionSource<object> completion, out Task returnTask, bool asyncWrite)
        {
            // Fetch reader with input params
            Task fetchInputParameterEncryptionInfoTask = null;
            bool describeParameterEncryptionNeeded = false;
            SqlDataReader describeParameterEncryptionDataReader = null;
            returnTask = null;

            Debug.Assert(_activeConnection != null, "_activeConnection should not be null in PrepareForTransparentEncryption.");
            Debug.Assert(_activeConnection.Parser != null, "_activeConnection.Parser should not be null in PrepareForTransparentEncryption.");
            Debug.Assert(_activeConnection.Parser.IsColumnEncryptionSupported,
                "_activeConnection.Parser.IsColumnEncryptionSupported should be true in PrepareForTransparentEncryption.");
            Debug.Assert(_columnEncryptionSetting == SqlCommandColumnEncryptionSetting.Enabled
                        || (_columnEncryptionSetting == SqlCommandColumnEncryptionSetting.UseConnectionSetting && _activeConnection.IsColumnEncryptionSettingEnabled),
                        "ColumnEncryption setting should be enabled for input parameter encryption.");
            Debug.Assert(async == (completion != null), "completion should can be null if and only if mode is async.");

            // A flag to indicate if finallyblock needs to execute.
            bool processFinallyBlock = true;

            // A flag to indicate if we need to decrement async count on the connection in finally block.
            bool decrementAsyncCountInFinallyBlock = async;

            // Flag to indicate if exception is caught during the execution, to govern clean up.
            bool exceptionCaught = false;

            // Used in BatchRPCMode to maintain a map of describe parameter encryption RPC requests (Keys) and their corresponding original RPC requests (Values).
            ReadOnlyDictionary<_SqlRPC, _SqlRPC> describeParameterEncryptionRpcOriginalRpcMap = null;

            TdsParser bestEffortCleanupTarget = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                {
#endif //DEBUG
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
                    try {
                        // Fetch the encryption information that applies to any of the input parameters.
                        describeParameterEncryptionDataReader = TryFetchInputParameterEncryptionInfo(timeout,
                                                                                                     async,
                                                                                                     asyncWrite,
                                                                                                     out describeParameterEncryptionNeeded,
                                                                                                     out fetchInputParameterEncryptionInfoTask,
                                                                                                     out describeParameterEncryptionRpcOriginalRpcMap);

                        Debug.Assert(describeParameterEncryptionNeeded || describeParameterEncryptionDataReader == null,
                            "describeParameterEncryptionDataReader should be null if we don't need to request describe parameter encryption request.");

                        Debug.Assert(fetchInputParameterEncryptionInfoTask == null || async,
                            "Task returned by TryFetchInputParameterEncryptionInfo, when in sync mode, in PrepareForTransparentEncryption.");

                        Debug.Assert((describeParameterEncryptionRpcOriginalRpcMap != null) == BatchRPCMode,
                            "describeParameterEncryptionRpcOriginalRpcMap can be non-null if and only if it is in BatchRPCMode.");

                        // If we didn't have parameters, we can fall back to regular code path, by simply returning.
                        if (!describeParameterEncryptionNeeded) {
                            Debug.Assert(null == fetchInputParameterEncryptionInfoTask,
                                "fetchInputParameterEncryptionInfoTask should not be set if describe parameter encryption is not needed.");

                            Debug.Assert(null == describeParameterEncryptionDataReader,
                                "SqlDataReader created for describe parameter encryption params when it is not needed.");

                            return;
                        }

                        Debug.Assert(describeParameterEncryptionDataReader != null,
                            "describeParameterEncryptionDataReader should not be null, as it is required to get results of describe parameter encryption.");

                        // Fire up another task to read the results of describe parameter encryption
                        if (fetchInputParameterEncryptionInfoTask != null) {
                            // Mark that we should not process the finally block since we have async execution pending.
                            // Note that this should be done outside the task's continuation delegate.
                            processFinallyBlock = false;
                            returnTask = AsyncHelper.CreateContinuationTask(fetchInputParameterEncryptionInfoTask, () => {
                                bool processFinallyBlockAsync = true;

                                RuntimeHelpers.PrepareConstrainedRegions();
                                try {
#if DEBUG
                                    TdsParser.ReliabilitySection tdsReliabilitySectionAsync = new TdsParser.ReliabilitySection();
                                    RuntimeHelpers.PrepareConstrainedRegions();
                                    try {
                                        tdsReliabilitySectionAsync.Start();
#endif //DEBUG
                                        // Check for any exceptions on network write, before reading.
                                        CheckThrowSNIException();

                                        // If it is async, then TryFetchInputParameterEncryptionInfo-> RunExecuteReaderTds would have incremented the async count.
                                        // Decrement it when we are about to complete async execute reader.
                                        SqlInternalConnectionTds internalConnectionTds = _activeConnection.GetOpenTdsConnection();
                                        if (internalConnectionTds != null)
                                        {
                                            internalConnectionTds.DecrementAsyncCount();
                                            decrementAsyncCountInFinallyBlock = false;
                                        }

                                        // Complete executereader.
                                        describeParameterEncryptionDataReader = CompleteAsyncExecuteReader();
                                        Debug.Assert(null == _stateObj, "non-null state object in PrepareForTransparentEncryption.");

                                        // Read the results of describe parameter encryption.
                                        ReadDescribeEncryptionParameterResults(describeParameterEncryptionDataReader, describeParameterEncryptionRpcOriginalRpcMap);

#if DEBUG
                                        // Failpoint to force the thread to halt to simulate cancellation of SqlCommand.
                                        if (_sleepAfterReadDescribeEncryptionParameterResults) {
                                            Thread.Sleep(10000);
                                        }
                                    }
                                    finally {
                                        tdsReliabilitySectionAsync.Stop();
                                    }
#endif //DEBUG
                                }
                                catch (Exception e) {
                                    processFinallyBlockAsync = ADP.IsCatchableExceptionType(e);
                                    throw;
                                }
                                finally {
                                    PrepareTransparentEncryptionFinallyBlock(   closeDataReader: processFinallyBlockAsync,
                                                                                decrementAsyncCount: decrementAsyncCountInFinallyBlock,
                                                                                clearDataStructures: processFinallyBlockAsync,
                                                                                wasDescribeParameterEncryptionNeeded: describeParameterEncryptionNeeded,
                                                                                describeParameterEncryptionRpcOriginalRpcMap: describeParameterEncryptionRpcOriginalRpcMap,
                                                                                describeParameterEncryptionDataReader: describeParameterEncryptionDataReader);
                                }
                            }, 
                            onFailure: ((exception) => {
                            if (_cachedAsyncState != null) {
                                _cachedAsyncState.ResetAsyncState();
                            }
                            if (exception != null) {
                                throw exception;
                            }}));
                        }
                        else {
                            // If it was async, ending the reader is still pending.
                            if (async) {
                                // Mark that we should not process the finally block since we have async execution pending.
                                // Note that this should be done outside the task's continuation delegate.
                                processFinallyBlock = false;
                                returnTask = Task.Run(() => {
                                        bool processFinallyBlockAsync = true;

                                        RuntimeHelpers.PrepareConstrainedRegions();
                                        try {
#if DEBUG
                                            TdsParser.ReliabilitySection tdsReliabilitySectionAsync = new TdsParser.ReliabilitySection();
                                            RuntimeHelpers.PrepareConstrainedRegions();
                                            try {
                                                tdsReliabilitySectionAsync.Start();
#endif //DEBUG

                                                // Check for any exceptions on network write, before reading.
                                                CheckThrowSNIException();

                                                // If it is async, then TryFetchInputParameterEncryptionInfo-> RunExecuteReaderTds would have incremented the async count.
                                                // Decrement it when we are about to complete async execute reader.
                                                SqlInternalConnectionTds internalConnectionTds = _activeConnection.GetOpenTdsConnection();
                                                if (internalConnectionTds != null) {
                                                    internalConnectionTds.DecrementAsyncCount();
                                                    decrementAsyncCountInFinallyBlock = false;
                                                }

                                                // Complete executereader.
                                                describeParameterEncryptionDataReader = CompleteAsyncExecuteReader();
                                                Debug.Assert(null == _stateObj, "non-null state object in PrepareForTransparentEncryption.");

                                                // Read the results of describe parameter encryption.
                                                ReadDescribeEncryptionParameterResults(describeParameterEncryptionDataReader, describeParameterEncryptionRpcOriginalRpcMap);
#if DEBUG
                                                // Failpoint to force the thread to halt to simulate cancellation of SqlCommand.
                                                if (_sleepAfterReadDescribeEncryptionParameterResults) {
                                                    Thread.Sleep(10000);
                                                }
#endif
#if DEBUG
                                            }
                                            finally {
                                                tdsReliabilitySectionAsync.Stop();
                                            }
#endif //DEBUG
                                        }
                                        catch (Exception e) {
                                            processFinallyBlockAsync = ADP.IsCatchableExceptionType(e);
                                            throw;
                                        }
                                        finally {
                                            PrepareTransparentEncryptionFinallyBlock(   closeDataReader: processFinallyBlockAsync,
                                                                                        decrementAsyncCount: decrementAsyncCountInFinallyBlock,
                                                                                        clearDataStructures: processFinallyBlockAsync,
                                                                                        wasDescribeParameterEncryptionNeeded: describeParameterEncryptionNeeded,
                                                                                        describeParameterEncryptionRpcOriginalRpcMap: describeParameterEncryptionRpcOriginalRpcMap,
                                                                                        describeParameterEncryptionDataReader: describeParameterEncryptionDataReader);
                                        }
                                    });
                            }
                            else {
                                // For synchronous execution, read the results of describe parameter encryption here.
                                ReadDescribeEncryptionParameterResults(describeParameterEncryptionDataReader, describeParameterEncryptionRpcOriginalRpcMap);
                            }

#if DEBUG
                            // Failpoint to force the thread to halt to simulate cancellation of SqlCommand.
                            if (_sleepAfterReadDescribeEncryptionParameterResults) {
                                Thread.Sleep(10000);
                            }
#endif
                        }
                    }
                    catch (Exception e) {
                        processFinallyBlock = ADP.IsCatchableExceptionType(e);
                        exceptionCaught = true;
                        throw;
                    }
                    finally {
                        // Free up the state only for synchronous execution. For asynchronous execution, free only if there was an exception.
                        PrepareTransparentEncryptionFinallyBlock(closeDataReader: (processFinallyBlock &&  !async) || exceptionCaught,
                                               decrementAsyncCount: decrementAsyncCountInFinallyBlock && exceptionCaught,
                                               clearDataStructures: (processFinallyBlock && !async) || exceptionCaught,
                                               wasDescribeParameterEncryptionNeeded: describeParameterEncryptionNeeded,
                                               describeParameterEncryptionRpcOriginalRpcMap: describeParameterEncryptionRpcOriginalRpcMap,
                                               describeParameterEncryptionDataReader: describeParameterEncryptionDataReader);
                    }
                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif //DEBUG
            }
            catch (System.OutOfMemoryException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.StackOverflowException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.Threading.ThreadAbortException e) {
                _activeConnection.Abort(e);
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                throw;
            }
            catch (Exception e) {
                if (cachedAsyncState != null) {
                    cachedAsyncState.ResetAsyncState();
                }

                if (ADP.IsCatchableExceptionType(e)) {
                    ReliablePutStateObject();
                }

                throw;
            }
        }

        /// <summary>
        /// Executes an RPC to fetch param encryption info from SQL Engine. If this method is not done writing
        ///  the request to wire, it'll set the "task" parameter which can be used to create continuations.
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="async"></param>
        /// <param name="asyncWrite"></param>
        /// <param name="inputParameterEncryptionNeeded"></param>
        /// <param name="task"></param>
        /// <param name="describeParameterEncryptionRpcOriginalRpcMap"></param>
        /// <returns></returns>
        private SqlDataReader TryFetchInputParameterEncryptionInfo(int timeout,
                                                                   bool async,
                                                                   bool asyncWrite,
                                                                   out bool inputParameterEncryptionNeeded,
                                                                   out Task task,
                                                                   out ReadOnlyDictionary<_SqlRPC, _SqlRPC> describeParameterEncryptionRpcOriginalRpcMap) {
            inputParameterEncryptionNeeded = false;
            task = null;
            describeParameterEncryptionRpcOriginalRpcMap = null;

            if (BatchRPCMode) {
                // Count the rpc requests that need to be transparently encrypted
                // We simply look for any parameters in a request and add the request to be queried for parameter encryption
                Dictionary<_SqlRPC, _SqlRPC> describeParameterEncryptionRpcOriginalRpcDictionary = new Dictionary<_SqlRPC, _SqlRPC>();

                for (int i = 0; i < _SqlRPCBatchArray.Length; i++) {
                    // In BatchRPCMode, the actual T-SQL query is in the first parameter and not present as the rpcName, as is the case with non-BatchRPCMode.
                    // So input parameters start at parameters[1]. parameters[0] is the actual T-SQL Statement. rpcName is sp_executesql.
                    if (_SqlRPCBatchArray[i].parameters.Length > 1) {
                        _SqlRPCBatchArray[i].needsFetchParameterEncryptionMetadata = true;

                        // Since we are going to need multiple RPC objects, allocate a new one here for each command in the batch.
                        _SqlRPC rpcDescribeParameterEncryptionRequest = new _SqlRPC();

                        // Prepare the describe parameter encryption request.
                        PrepareDescribeParameterEncryptionRequest(_SqlRPCBatchArray[i], ref rpcDescribeParameterEncryptionRequest);
                        Debug.Assert(rpcDescribeParameterEncryptionRequest != null, "rpcDescribeParameterEncryptionRequest should not be null, after call to PrepareDescribeParameterEncryptionRequest.");

                        Debug.Assert(!describeParameterEncryptionRpcOriginalRpcDictionary.ContainsKey(rpcDescribeParameterEncryptionRequest),
                            "There should not already be a key referring to the current rpcDescribeParameterEncryptionRequest, in the dictionary describeParameterEncryptionRpcOriginalRpcDictionary.");

                        // Add the describe parameter encryption RPC request as the key and its corresponding original rpc request to the dictionary.
                        describeParameterEncryptionRpcOriginalRpcDictionary.Add(rpcDescribeParameterEncryptionRequest, _SqlRPCBatchArray[i]);
                    }
                }

                describeParameterEncryptionRpcOriginalRpcMap = new ReadOnlyDictionary<_SqlRPC, _SqlRPC>(describeParameterEncryptionRpcOriginalRpcDictionary);

                if (describeParameterEncryptionRpcOriginalRpcMap.Count == 0) {
                    // If no parameters are present, nothing to do, simply return.
                    return null;
                }
                else {
                    inputParameterEncryptionNeeded = true;
                }

                _sqlRPCParameterEncryptionReqArray = describeParameterEncryptionRpcOriginalRpcMap.Keys.ToArray();

                Debug.Assert(_sqlRPCParameterEncryptionReqArray.Length > 0, "There should be at-least 1 describe parameter encryption rpc request.");
                Debug.Assert(_sqlRPCParameterEncryptionReqArray.Length <= _SqlRPCBatchArray.Length,
                                "The number of decribe parameter encryption RPC requests is more than the number of original RPC requests.");
            }
            else if (0 != GetParameterCount(_parameters)) {
                // Fetch params for a single batch
                inputParameterEncryptionNeeded = true;
                _sqlRPCParameterEncryptionReqArray = new _SqlRPC[1];

                _SqlRPC rpc = null;
                GetRPCObject(_parameters.Count, ref rpc);
                Debug.Assert(rpc != null, "GetRPCObject should not return rpc as null.");

                rpc.rpcName = CommandText;

                int i = 0;
                foreach (SqlParameter sqlParam in _parameters) {
                    rpc.parameters[i++] = sqlParam;
                }

                // Prepare the RPC request for describe parameter encryption procedure.
                PrepareDescribeParameterEncryptionRequest(rpc, ref _sqlRPCParameterEncryptionReqArray[0]);
                Debug.Assert(_sqlRPCParameterEncryptionReqArray[0] != null, "_sqlRPCParameterEncryptionReqArray[0] should not be null, after call to PrepareDescribeParameterEncryptionRequest.");
            }

            if (inputParameterEncryptionNeeded) {
                // Set the flag that indicates that parameter encryption requests are currently in-progress.
                _isDescribeParameterEncryptionRPCCurrentlyInProgress = true;

#if DEBUG
                // Failpoint to force the thread to halt to simulate cancellation of SqlCommand.
                if (_sleepDuringTryFetchInputParameterEncryptionInfo) {
                    Thread.Sleep(10000);
                }
#endif

                // Execute the RPC.
                return RunExecuteReaderTds( CommandBehavior.Default,
                                            runBehavior: RunBehavior.ReturnImmediately, // Other RunBehavior modes will skip reading rows.
                                            returnStream: true,
                                            async: async,
                                            timeout: timeout,
                                            task: out task,
                                            asyncWrite: asyncWrite,
                                            ds: null,
                                            describeParameterEncryptionRequest: true);
            }
            else {
                return null;
            }
        }

        /// <summary>
        /// Constructs a SqlParameter with a given string value
        /// </summary>
        /// <param name="queryText"></param>
        /// <returns></returns>
        private SqlParameter GetSqlParameterWithQueryText(string queryText)
        {
            SqlParameter sqlParam = new SqlParameter(null, ((queryText.Length << 1) <= TdsEnums.TYPE_SIZE_LIMIT) ? SqlDbType.NVarChar : SqlDbType.NText, queryText.Length);
            sqlParam.Value = queryText;

            return sqlParam;
        }

        /// <summary>
        /// Constructs the sp_describe_parameter_encryption request with the values from the original RPC call.
        /// Prototype for <sp_describe_parameter_encryption> is 
        /// exec sp_describe_parameter_encryption @tsql=N'[SQL Statement]', @params=N'@p1 varbinary(256)'
        /// </summary>
        /// <param name="originalRpcRequest">Original RPC request</param>
        /// <param name="describeParameterEncryptionRequest">sp_describe_parameter_encryption request being built</param>
        private void PrepareDescribeParameterEncryptionRequest(_SqlRPC originalRpcRequest, ref _SqlRPC describeParameterEncryptionRequest) {
            Debug.Assert(originalRpcRequest != null);

            // Construct the RPC request for sp_describe_parameter_encryption
            // sp_describe_parameter_encryption always has 2 parameters (stmt, paramlist).
            GetRPCObject(2, ref describeParameterEncryptionRequest, forSpDescribeParameterEncryption:true);
            describeParameterEncryptionRequest.rpcName = "sp_describe_parameter_encryption";

            // Prepare @tsql parameter
            SqlParameter sqlParam;
            string text;

            // In BatchRPCMode, The actual T-SQL query is in the first parameter and not present as the rpcName, as is the case with non-BatchRPCMode.
            if (BatchRPCMode) {
                Debug.Assert(originalRpcRequest.parameters != null && originalRpcRequest.parameters.Length > 0,
                    "originalRpcRequest didn't have at-least 1 parameter in BatchRPCMode, in PrepareDescribeParameterEncryptionRequest.");
                text = (string)originalRpcRequest.parameters[0].Value;
                sqlParam = GetSqlParameterWithQueryText(text);
            }
            else {
                text = originalRpcRequest.rpcName;
                if (CommandType == Data.CommandType.StoredProcedure) {
                    // For stored procedures, we need to prepare @tsql in the following format
                    // N'EXEC sp_name @param1=@param1, @param1=@param2, ..., @paramN=@paramN'
                    sqlParam = BuildStoredProcedureStatementForColumnEncryption(text, originalRpcRequest.parameters);
                }
                else {
                    sqlParam = GetSqlParameterWithQueryText(text);
                }
            }

            Debug.Assert(text != null, "@tsql parameter is null in PrepareDescribeParameterEncryptionRequest.");

            describeParameterEncryptionRequest.parameters[0] = sqlParam;
            string parameterList = null;

            // In BatchRPCMode, the input parameters start at parameters[1]. parameters[0] is the T-SQL statement. rpcName is sp_executesql.
            // And it is already in the format expected out of BuildParamList, which is not the case with Non-BatchRPCMode.
            if (BatchRPCMode) {
                if (originalRpcRequest.parameters.Length > 1) {
                    parameterList = (string)originalRpcRequest.parameters[1].Value;
                }
            }
            else {
                // Prepare @params parameter
                // Need to create new parameters as we cannot have the same parameter being part of two SqlCommand objects
                SqlParameter paramCopy;
                SqlParameterCollection tempCollection = new SqlParameterCollection();

                for (int i = 0; i < _parameters.Count; i++) {
                    SqlParameter param = originalRpcRequest.parameters[i];
                    paramCopy = new SqlParameter(param.ParameterName, param.SqlDbType, param.Size, param.Direction, param.Precision, param.Scale, param.SourceColumn, param.SourceVersion,
                        param.SourceColumnNullMapping, param.Value, param.XmlSchemaCollectionDatabase, param.XmlSchemaCollectionOwningSchema, param.XmlSchemaCollectionName);
                    tempCollection.Add(paramCopy);
                }

                Debug.Assert(_stateObj == null, "_stateObj should be null at this time, in PrepareDescribeParameterEncryptionRequest.");
                Debug.Assert(_activeConnection != null, "_activeConnection should not be null at this time, in PrepareDescribeParameterEncryptionRequest.");
                TdsParser tdsParser = null;

                if (_activeConnection.Parser != null) {
                    tdsParser = _activeConnection.Parser;
                    if ((tdsParser == null) || (tdsParser.State == TdsParserState.Broken) || (tdsParser.State == TdsParserState.Closed)) {
                        // Connection's parser is null as well, therefore we must be closed
                        throw ADP.ClosedConnectionError();
                    }
                }

                parameterList = BuildParamList(tdsParser, tempCollection, includeReturnValue:true);
            }

            Debug.Assert(!string.IsNullOrWhiteSpace(parameterList), "parameterList should not be null or empty or whitespace.");

            sqlParam = new SqlParameter(null, ((parameterList.Length << 1) <= TdsEnums.TYPE_SIZE_LIMIT) ? SqlDbType.NVarChar : SqlDbType.NText, parameterList.Length);
            sqlParam.Value = parameterList;
            describeParameterEncryptionRequest.parameters[1] = sqlParam;
        }

        /// <summary>
        /// Read the output of sp_describe_parameter_encryption
        /// </summary>
        /// <param name="ds">Resultset from calling to sp_describe_parameter_encryption</param>
        /// <param name="describeParameterEncryptionRpcOriginalRpcMap"> Readonly dictionary with the map of parameter encryption rpc requests with the corresponding original rpc requests.</param>
        private void ReadDescribeEncryptionParameterResults(SqlDataReader ds, ReadOnlyDictionary<_SqlRPC, _SqlRPC> describeParameterEncryptionRpcOriginalRpcMap) {
            _SqlRPC rpc = null;
            int currentOrdinal = -1;
            SqlTceCipherInfoEntry cipherInfoEntry;
            Dictionary<int, SqlTceCipherInfoEntry> columnEncryptionKeyTable = new Dictionary<int, SqlTceCipherInfoEntry>();

            Debug.Assert((describeParameterEncryptionRpcOriginalRpcMap != null) == BatchRPCMode,
                "describeParameterEncryptionRpcOriginalRpcMap should be non-null if and only if it is BatchRPCMode.");

            // Indicates the current result set we are reading, used in BatchRPCMode, where we can have more than 1 result set.
            int resultSetSequenceNumber = 0;

#if DEBUG
            // Keep track of the number of rows in the result sets.
            int rowsAffected = 0;
#endif

            // A flag that used in BatchRPCMode, to assert the result of lookup in to the dictionary maintaining the map of describe parameter encryption requests
            // and the corresponding original rpc requests.
            bool lookupDictionaryResult;

            do {
                if (BatchRPCMode) {
                    // If we got more RPC results from the server than what was requested.
                    if (resultSetSequenceNumber >= _sqlRPCParameterEncryptionReqArray.Length) {
                        Debug.Assert(false, "Server sent back more results than what was expected for describe parameter encryption requests in BatchRPCMode.");
                        // Ignore the rest of the results from the server, if for whatever reason it sends back more than what we expect.
                        break;
                    }
                }

                // First read the column encryption key list
                while (ds.Read()) {

#if DEBUG
                    rowsAffected++;
#endif

                    // Column Encryption Key Ordinal.
                    currentOrdinal = ds.GetInt32((int)DescribeParameterEncryptionResultSet1.KeyOrdinal);
                    Debug.Assert(currentOrdinal >= 0, "currentOrdinal cannot be negative.");

                    // Try to see if there was already an entry for the current ordinal.
                    if (!columnEncryptionKeyTable.TryGetValue(currentOrdinal, out cipherInfoEntry)) {
                        // If an entry for this ordinal was not found, create an entry in the columnEncryptionKeyTable for this ordinal.
                        cipherInfoEntry = new SqlTceCipherInfoEntry(currentOrdinal);
                        columnEncryptionKeyTable.Add(currentOrdinal, cipherInfoEntry);
                    }

                    Debug.Assert(!cipherInfoEntry.Equals(default(SqlTceCipherInfoEntry)), "cipherInfoEntry should not be un-initialized.");

                    // Read the CEK.
                    byte[] encryptedKey = null;
                    int encryptedKeyLength = (int)ds.GetBytes((int)DescribeParameterEncryptionResultSet1.EncryptedKey, 0, encryptedKey, 0, 0);
                    encryptedKey = new byte[encryptedKeyLength];
                    ds.GetBytes((int)DescribeParameterEncryptionResultSet1.EncryptedKey, 0, encryptedKey, 0, encryptedKeyLength);

                    // Read the metadata version of the key.
                    // It should always be 8 bytes.
                    byte[] keyMdVersion = new byte[8];
                    ds.GetBytes((int)DescribeParameterEncryptionResultSet1.KeyMdVersion, 0, keyMdVersion, 0, keyMdVersion.Length);

                    // Validate the provider name
                    string providerName = ds.GetString((int)DescribeParameterEncryptionResultSet1.ProviderName);
                    //SqlColumnEncryptionKeyStoreProvider keyStoreProvider;
                    //if (!SqlConnection.TryGetColumnEncryptionKeyStoreProvider (providerName, out keyStoreProvider)) {
                    //    // unknown provider, skip processing this cek.
                    //    Bid.Trace("<sc.SqlCommand.ReadDescribeEncryptionParameterResults|INFO>Unknown provider name recevied %s, skipping\n", providerName);
                    //    continue;
                    //}

                    cipherInfoEntry.Add(encryptedKey: encryptedKey,
                                        databaseId: ds.GetInt32((int)DescribeParameterEncryptionResultSet1.DbId),
                                        cekId: ds.GetInt32((int)DescribeParameterEncryptionResultSet1.KeyId),
                                        cekVersion: ds.GetInt32((int)DescribeParameterEncryptionResultSet1.KeyVersion),
                                        cekMdVersion: keyMdVersion,
                                        keyPath: ds.GetString((int)DescribeParameterEncryptionResultSet1.KeyPath),
                                        keyStoreName: providerName,
                                        algorithmName: ds.GetString((int)DescribeParameterEncryptionResultSet1.KeyEncryptionAlgorithm));
                }

                if (!ds.NextResult()) {
                    throw SQL.UnexpectedDescribeParamFormat ();
                }

                // Find the RPC command that generated this tce request
                if (BatchRPCMode) {
                    Debug.Assert(_sqlRPCParameterEncryptionReqArray[resultSetSequenceNumber] != null, "_sqlRPCParameterEncryptionReqArray[resultSetSequenceNumber] should not be null.");

                    // Lookup in the dictionary to get the original rpc request corresponding to the describe parameter encryption request
                    // pointed to by _sqlRPCParameterEncryptionReqArray[resultSetSequenceNumber]
                    rpc = null;
                    lookupDictionaryResult = describeParameterEncryptionRpcOriginalRpcMap.TryGetValue(_sqlRPCParameterEncryptionReqArray[resultSetSequenceNumber++], out rpc);

                    Debug.Assert(lookupDictionaryResult,
                        "Describe Parameter Encryption RPC request key must be present in the dictionary describeParameterEncryptionRpcOriginalRpcMap");
                    Debug.Assert(rpc != null,
                        "Describe Parameter Encryption RPC request's corresponding original rpc request must not be null in the dictionary describeParameterEncryptionRpcOriginalRpcMap");
                }
                else {
                    rpc = _rpcArrayOf1[0];
                }

                Debug.Assert(rpc != null, "rpc should not be null here.");

                // This is the index in the parameters array where the actual parameters start.
                // In BatchRPCMode, parameters[0] has the t-sql, parameters[1] has the param list
                // and actual parameters of the query start at parameters[2].
                int parameterStartIndex = (BatchRPCMode ? 2 : 0);

                // Iterate over the parameter names to read the encryption type info
                int paramIdx;
                while (ds.Read()) {
#if DEBUG
                    rowsAffected++;
#endif
                    Debug.Assert(rpc != null, "Describe Parameter Encryption requested for non-tce spec proc");
                    string parameterName = ds.GetString((int)DescribeParameterEncryptionResultSet2.ParameterName);

                    // When the RPC object gets reused, the parameter array has more parameters that the valid params for the command.
                    // Null is used to indicate the end of the valid part of the array. Refer to GetRPCObject().
                    for (paramIdx = parameterStartIndex; paramIdx < rpc.parameters.Length && rpc.parameters[paramIdx] != null; paramIdx++) {
                        SqlParameter sqlParameter = rpc.parameters[paramIdx];
                        Debug.Assert(sqlParameter != null, "sqlParameter should not be null.");

                        if (sqlParameter.ParameterNameFixed.Equals(parameterName, StringComparison.Ordinal)) {
                            Debug.Assert(sqlParameter.CipherMetadata == null, "param.CipherMetadata should be null.");
                            sqlParameter.HasReceivedMetadata = true;

                            // Found the param, setup the encryption info.
                            byte columnEncryptionType = ds.GetByte((int)DescribeParameterEncryptionResultSet2.ColumnEncrytionType);
                            if ((byte)SqlClientEncryptionType.PlainText != columnEncryptionType) {
                                byte cipherAlgorithmId = ds.GetByte((int)DescribeParameterEncryptionResultSet2.ColumnEncryptionAlgorithm);
                                int columnEncryptionKeyOrdinal = ds.GetInt32((int)DescribeParameterEncryptionResultSet2.ColumnEncryptionKeyOrdinal);
                                byte columnNormalizationRuleVersion = ds.GetByte((int)DescribeParameterEncryptionResultSet2.NormalizationRuleVersion);

                                // Lookup the key, failing which throw an exception
                                if (!columnEncryptionKeyTable.TryGetValue(columnEncryptionKeyOrdinal, out cipherInfoEntry)) {
                                    throw SQL.InvalidEncryptionKeyOrdinal(columnEncryptionKeyOrdinal, columnEncryptionKeyTable.Count);
                                }

                                sqlParameter.CipherMetadata = new SqlCipherMetadata(sqlTceCipherInfoEntry: cipherInfoEntry,
                                                                                    ordinal: unchecked((ushort)-1),
                                                                                    cipherAlgorithmId: cipherAlgorithmId,
                                                                                    cipherAlgorithmName: null,
                                                                                    encryptionType: columnEncryptionType,
                                                                                    normalizationRuleVersion: columnNormalizationRuleVersion);

                                // Decrypt the symmetric key.(This will also validate and throw if needed).
                                Debug.Assert(_activeConnection != null, @"_activeConnection should not be null");
                                SqlSecurityUtility.DecryptSymmetricKey(sqlParameter.CipherMetadata, this._activeConnection.DataSource);

                                // This is effective only for BatchRPCMode even though we set it for non-BatchRPCMode also,
                                // since for non-BatchRPCMode mode, paramoptions gets thrown away and reconstructed in BuildExecuteSql.
                                rpc.paramoptions[paramIdx] |= TdsEnums.RPC_PARAM_ENCRYPTED;
                            }

                            break;
                        }
                    }
                }

                // When the RPC object gets reused, the parameter array has more parameters that the valid params for the command.
                // Null is used to indicate the end of the valid part of the array. Refer to GetRPCObject().
                for (paramIdx = parameterStartIndex; paramIdx < rpc.parameters.Length && rpc.parameters[paramIdx] != null; paramIdx++) {
                    if (!rpc.parameters[paramIdx].HasReceivedMetadata && rpc.parameters[paramIdx].Direction != ParameterDirection.ReturnValue) {
                        // Encryption MD wasn't sent by the server - we expect the metadata to be sent for all the parameters 
                        // that were sent in the original sp_describe_parameter_encryption but not necessarily for return values,
                        // since there might be multiple return values but server will only send for one of them.
                        // For parameters that don't need encryption, the encryption type is set to plaintext.
                        throw SQL.ParamEncryptionMetadataMissing(rpc.parameters[paramIdx].ParameterName, rpc.GetCommandTextOrRpcName());
                    }
                }

#if DEBUG
                Debug.Assert(rowsAffected == RowsAffectedByDescribeParameterEncryption,
                            "number of rows received for describe parameter encryption should be equal to rows affected by describe parameter encryption.");
#endif

                 // The server has responded with encryption related information for this rpc request. So clear the needsFetchParameterEncryptionMetadata flag.
                rpc.needsFetchParameterEncryptionMetadata = false;
            } while (ds.NextResult());

            // Verify that we received response for each rpc call needs tce
            if (BatchRPCMode) {
                for (int i = 0; i < _SqlRPCBatchArray.Length; i++) {
                    if (_SqlRPCBatchArray[i].needsFetchParameterEncryptionMetadata) {
                        throw SQL.ProcEncryptionMetadataMissing(_SqlRPCBatchArray[i].rpcName);
                    }
                }
            }
        }

        internal SqlDataReader RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, bool returnStream, string method) {
            Task unused; // sync execution 
            SqlDataReader reader = RunExecuteReader(cmdBehavior, runBehavior, returnStream, method, completion:null, timeout:CommandTimeout, task:out unused);
            Debug.Assert(unused == null, "returned task during synchronous execution");
            return reader;
        }

        // task is created in case of pending asynchronous write, returned SqlDataReader should not be utilized until that task is complete 
        internal SqlDataReader RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, bool returnStream, string method, TaskCompletionSource<object> completion, int timeout, out Task task, bool asyncWrite = false) {
            bool async = (null != completion);

            task = null;

            _rowsAffected = -1;
            _rowsAffectedBySpDescribeParameterEncryption = -1;
            
            if (0 != (CommandBehavior.SingleRow & cmdBehavior)) {
                // CommandBehavior.SingleRow implies CommandBehavior.SingleResult
                cmdBehavior |= CommandBehavior.SingleResult;
            }

            // @devnote: this function may throw for an invalid connection
            // @devnote: returns false for empty command text
            ValidateCommand(method, async);
            CheckNotificationStateAndAutoEnlist(); // Only call after validate - requires non null connection!

            TdsParser bestEffortCleanupTarget = null;
            // This section needs to occur AFTER ValidateCommand - otherwise it will AV without a connection.
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                {
#endif //DEBUG
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
                    SqlStatistics statistics = Statistics;
                    if (null != statistics) {
                        if ((!this.IsDirty && this.IsPrepared && !_hiddenPrepare)
                            || (this.IsPrepared && _execType == EXECTYPE.PREPAREPENDING))
                        {
                            statistics.SafeIncrement(ref statistics._preparedExecs);
                        }
                        else {
                            statistics.SafeIncrement(ref statistics._unpreparedExecs);
                        }
                    }

                    // Reset the encryption related state of the command and its parameters.
                    ResetEncryptionState();

                    if ( _activeConnection.IsContextConnection ) {
                        return RunExecuteReaderSmi( cmdBehavior, runBehavior, returnStream );
                    }
                    else if (IsColumnEncryptionEnabled) {
                        Task returnTask = null;
                        PrepareForTransparentEncryption(cmdBehavior, returnStream, async, timeout, completion, out returnTask, asyncWrite && async);
                        Debug.Assert(async == (returnTask != null), @"returnTask should be null if and only if async is false.");

                        return RunExecuteReaderTdsWithTransparentParameterEncryption( cmdBehavior, runBehavior, returnStream, async, timeout, out task, asyncWrite && async, ds: null,
                            describeParameterEncryptionRequest: false, describeParameterEncryptionTask: returnTask);
                    }
                    else {
                        return RunExecuteReaderTds( cmdBehavior, runBehavior, returnStream, async, timeout, out task, asyncWrite && async);
                    }

                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif //DEBUG
            }
            catch (System.OutOfMemoryException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.StackOverflowException e) {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.Threading.ThreadAbortException e)  {
                _activeConnection.Abort(e);
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                throw;
            }
        }

        /// <summary>
        /// RunExecuteReaderTds after Transparent Parameter Encryption is complete.
        /// </summary>
        /// <param name="cmdBehavior"></param>
        /// <param name="runBehavior"></param>
        /// <param name="returnStream"></param>
        /// <param name="async"></param>
        /// <param name="timeout"></param>
        /// <param name="task"></param>
        /// <param name="asyncWrite"></param>
        /// <param name="ds"></param>
        /// <param name="describeParameterEncryptionRequest"></param>
        /// <param name="describeParameterEncryptionTask"></param>
        /// <returns></returns>
        private SqlDataReader RunExecuteReaderTdsWithTransparentParameterEncryption(CommandBehavior cmdBehavior,
                                                                                    RunBehavior runBehavior,
                                                                                    bool returnStream,
                                                                                    bool async,
                                                                                    int timeout,
                                                                                    out Task task,
                                                                                    bool asyncWrite,
                                                                                    SqlDataReader ds=null,
                                                                                    bool describeParameterEncryptionRequest = false,
                                                                                    Task describeParameterEncryptionTask = null) {
            Debug.Assert(!asyncWrite || async, "AsyncWrite should be always accompanied by Async");
            Debug.Assert((describeParameterEncryptionTask != null) == async, @"async should be true if and only if describeParameterEncryptionTask is not null.");

            if (ds == null && returnStream) {
                ds = new SqlDataReader(this, cmdBehavior);
            }

            if (describeParameterEncryptionTask != null) {
                long parameterEncryptionStart = ADP.TimerCurrent();
                    TaskCompletionSource<object> completion = new TaskCompletionSource<object>();
                    AsyncHelper.ContinueTask(describeParameterEncryptionTask, completion,
                        () => {
                            Task subTask = null;
                            RunExecuteReaderTds(cmdBehavior, runBehavior, returnStream, async, TdsParserStaticMethods.GetRemainingTimeout(timeout, parameterEncryptionStart), out subTask, asyncWrite, ds);
                            if (subTask == null) {
                                completion.SetResult(null);
                            }
                            else {
                                AsyncHelper.ContinueTask(subTask, completion, () => completion.SetResult(null));
                            }
                        }, connectionToDoom: null,
                        onFailure: ((exception) => {
                            if (_cachedAsyncState != null) {
                                _cachedAsyncState.ResetAsyncState();
                            }
                            if (exception != null) {
                                throw exception;
                            }}),
                        onCancellation: (() => {
                            if (_cachedAsyncState != null) {
                                _cachedAsyncState.ResetAsyncState();
                            }}),
                        connectionToAbort: _activeConnection);
                    task = completion.Task;
                    return ds;
            }
            else {
                // Synchronous execution.
                return RunExecuteReaderTds(cmdBehavior, runBehavior, returnStream, async, timeout, out task, asyncWrite, ds);
            }
        }

        private SqlDataReader RunExecuteReaderTds( CommandBehavior cmdBehavior, RunBehavior runBehavior, bool returnStream, bool async, int timeout, out Task task, bool asyncWrite, SqlDataReader ds=null, bool describeParameterEncryptionRequest = false) {
            Debug.Assert(!asyncWrite || async, "AsyncWrite should be always accompanied by Async");

            if (ds == null && returnStream) {
                ds = new SqlDataReader(this, cmdBehavior);
            }

            Task reconnectTask = _activeConnection.ValidateAndReconnect(null, timeout);

            if (reconnectTask != null) {
                long reconnectionStart = ADP.TimerCurrent();
                if (async) {                    
                    TaskCompletionSource<object> completion = new TaskCompletionSource<object>();
                    _activeConnection.RegisterWaitingForReconnect(completion.Task);
                    _reconnectionCompletionSource = completion;
                    CancellationTokenSource timeoutCTS = new CancellationTokenSource();
                    AsyncHelper.SetTimeoutException(completion, timeout, SQL.CR_ReconnectTimeout, timeoutCTS.Token);
                    AsyncHelper.ContinueTask(reconnectTask, completion,
                        () => {
                            if (completion.Task.IsCompleted) {
                                return;
                            }
                            Interlocked.CompareExchange(ref _reconnectionCompletionSource, null, completion);
                            timeoutCTS.Cancel();
                            Task subTask;                            
                            RunExecuteReaderTds(cmdBehavior, runBehavior, returnStream, async, TdsParserStaticMethods.GetRemainingTimeout(timeout, reconnectionStart), out subTask, asyncWrite, ds);
                            if (subTask == null) {
                                completion.SetResult(null);
                            }
                            else {
                                AsyncHelper.ContinueTask(subTask, completion, () => completion.SetResult(null));
                            }
                        }, connectionToAbort: _activeConnection);
                    task = completion.Task;
                    return ds;
                }
                else {
                    AsyncHelper.WaitForCompletion(reconnectTask, timeout, () => { throw SQL.CR_ReconnectTimeout(); });
                    timeout = TdsParserStaticMethods.GetRemainingTimeout(timeout, reconnectionStart);
                }
            }

            // make sure we have good parameter information
            // prepare the command
            // execute
            Debug.Assert(null != _activeConnection.Parser, "TdsParser class should not be null in Command.Execute!");

            bool inSchema =  (0 != (cmdBehavior & CommandBehavior.SchemaOnly));           

            // create a new RPC
            _SqlRPC rpc=null;

            task = null;

            string optionSettings = null;
            bool processFinallyBlock = true;
            bool decrementAsyncCountOnFailure = false;

            if (async) {
                _activeConnection.GetOpenTdsConnection().IncrementAsyncCount();
                decrementAsyncCountOnFailure = true;
            }

            try {
              
                if (asyncWrite) {
                    _activeConnection.AddWeakReference(this, SqlReferenceCollection.CommandTag);
                }

                GetStateObject();
                Task writeTask = null;

                if (describeParameterEncryptionRequest) {
#if DEBUG
                    if (_sleepDuringRunExecuteReaderTdsForSpDescribeParameterEncryption) {
                        Thread.Sleep(10000);
                    }
#endif

                    Debug.Assert(_sqlRPCParameterEncryptionReqArray != null, "RunExecuteReader rpc array not provided for describe parameter encryption request.");
                    writeTask = _stateObj.Parser.TdsExecuteRPC(this, _sqlRPCParameterEncryptionReqArray, timeout, inSchema, this.Notification, _stateObj, CommandType.StoredProcedure == CommandType, sync: !asyncWrite);
                }
                else if (BatchRPCMode) {
                    Debug.Assert(inSchema == false, "Batch RPC does not support schema only command beahvior");
                    Debug.Assert(!IsPrepared, "Batch RPC should not be prepared!");
                    Debug.Assert(!IsDirty, "Batch RPC should not be marked as dirty!");
                    //Currently returnStream is always false, but we may want to return a Reader later.
                    //if (returnStream) {
                    //    Bid.Trace("<sc.SqlCommand.ExecuteReader|INFO> %d#, Command executed as batch RPC.\n", ObjectID);
                    //}
                    Debug.Assert(_SqlRPCBatchArray != null, "RunExecuteReader rpc array not provided");
                    writeTask = _stateObj.Parser.TdsExecuteRPC(this, _SqlRPCBatchArray, timeout, inSchema, this.Notification, _stateObj, CommandType.StoredProcedure == CommandType, sync: !asyncWrite );                    
                }
                else if ((System.Data.CommandType.Text == this.CommandType) && (0 == GetParameterCount(_parameters))) {
                    // Send over SQL Batch command if we are not a stored proc and have no parameters
                    // MDAC 
                    Debug.Assert(!IsUserPrepared, "CommandType.Text with no params should not be prepared!");
                    if (returnStream) {
                        Bid.Trace("<sc.SqlCommand.ExecuteReader|INFO> %d#, Command executed as SQLBATCH.\n", ObjectID);
                    }
                    string text = GetCommandText(cmdBehavior) + GetResetOptionsString(cmdBehavior);
                    writeTask = _stateObj.Parser.TdsExecuteSQLBatch(text, timeout, this.Notification, _stateObj, sync: !asyncWrite);
                }
                else if (System.Data.CommandType.Text == this.CommandType) {
                    if (this.IsDirty) {
                        Debug.Assert(_cachedMetaData == null || !_dirty, "dirty query should not have cached metadata!"); // can have cached metadata if dirty because of parameters
                        //
                        // someone changed the command text or the parameter schema so we must unprepare the command
                        //
                        // remeber that IsDirty includes test for IsPrepared!
                        if(_execType == EXECTYPE.PREPARED) {
                            _hiddenPrepare = true;
                        }
                        Unprepare();
                        IsDirty = false;
                    }

                    if (_execType == EXECTYPE.PREPARED) {
                        Debug.Assert(this.IsPrepared && (_prepareHandle != -1), "invalid attempt to call sp_execute without a handle!");
                        rpc = BuildExecute(inSchema);
                    }
                    else if (_execType == EXECTYPE.PREPAREPENDING) {
                        Debug.Assert(_activeConnection.IsShiloh, "Invalid attempt to call sp_prepexec on non 7.x server");
                        rpc = BuildPrepExec(cmdBehavior);
                        // next time through, only do an exec
                        _execType = EXECTYPE.PREPARED;
                        _preparedConnectionCloseCount = _activeConnection.CloseCount;
                        _preparedConnectionReconnectCount = _activeConnection.ReconnectCount;
                        // mark ourselves as preparing the command
                        _inPrepare = true;
                    }
                    else {
                        Debug.Assert(_execType == EXECTYPE.UNPREPARED, "Invalid execType!");
                        BuildExecuteSql(cmdBehavior, null, _parameters, ref rpc);
                    }

                    // if shiloh, then set NOMETADATA_UNLESSCHANGED flag
                    if (_activeConnection.IsShiloh)
                        rpc.options = TdsEnums.RPC_NOMETADATA;
                    if (returnStream) {
                        Bid.Trace("<sc.SqlCommand.ExecuteReader|INFO> %d#, Command executed as RPC.\n", ObjectID);
                    }

                    // 
                    Debug.Assert(_rpcArrayOf1[0] == rpc);
                    writeTask = _stateObj.Parser.TdsExecuteRPC(this, _rpcArrayOf1, timeout, inSchema, this.Notification, _stateObj, CommandType.StoredProcedure == CommandType, sync:!asyncWrite);
                }
                else {
                    Debug.Assert(this.CommandType == System.Data.CommandType.StoredProcedure, "unknown command type!");
                    // note: invalid asserts on Shiloh. On 8.0 (Shiloh) and above a command is ALWAYS prepared
                    // and IsDirty is always set if there are changes and the command is marked Prepared!
                    Debug.Assert(IsShiloh || !IsPrepared, "RPC should not be prepared!");
                    Debug.Assert(IsShiloh || !IsDirty, "RPC should not be marked as dirty!");

                    BuildRPC(inSchema, _parameters, ref rpc);

                    // if we need to augment the command because a user has changed the command behavior (e.g. FillSchema)
                    // then batch sql them over.  This is inefficient (3 round trips) but the only way we can get metadata only from
                    // a stored proc
                    optionSettings = GetSetOptionsString(cmdBehavior);
                    if (returnStream) {
                        Bid.Trace("<sc.SqlCommand.ExecuteReader|INFO> %d#, Command executed as RPC.\n", ObjectID);
                    }
                    // turn set options ON
                    if (null != optionSettings) {
                        Task executeTask = _stateObj.Parser.TdsExecuteSQLBatch(optionSettings, timeout, this.Notification, _stateObj, sync: true);
                        Debug.Assert(executeTask == null, "Shouldn't get a task when doing sync writes");
                        bool dataReady;
                        Debug.Assert(_stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
                        bool result = _stateObj.Parser.TryRun(RunBehavior.UntilDone, this, null, null, _stateObj, out dataReady);
                        if (!result) { throw SQL.SynchronousCallMayNotPend(); }
                        // and turn OFF when the ds exhausts the stream on Close()
                        optionSettings = GetResetOptionsString(cmdBehavior);
                    }

                    // turn debugging on
                    _activeConnection.CheckSQLDebug();

                    // execute sp
                    Debug.Assert(_rpcArrayOf1[0] == rpc);
                    writeTask=_stateObj.Parser.TdsExecuteRPC(this, _rpcArrayOf1, timeout, inSchema, this.Notification, _stateObj, CommandType.StoredProcedure == CommandType, sync:!asyncWrite); 
                }

                Debug.Assert(writeTask == null || async, "Returned task in sync mode");

                if (async) {
                    decrementAsyncCountOnFailure = false; 
                    if (writeTask != null) {
                        task = AsyncHelper.CreateContinuationTask(writeTask, () => {
                                     _activeConnection.GetOpenTdsConnection(); // it will throw if connection is closed
                                     cachedAsyncState.SetAsyncReaderState(ds, runBehavior, optionSettings);
                                 },
                                 onFailure: (exc) => {
                                     _activeConnection.GetOpenTdsConnection().DecrementAsyncCount(); 
                                 } );
                    }
                    else {
                        cachedAsyncState.SetAsyncReaderState(ds, runBehavior, optionSettings);
                    }
                }
                else {
                    // Always execute - even if no reader!
                    FinishExecuteReader(ds, runBehavior, optionSettings);
                }
            }
            catch (Exception e) {                
                processFinallyBlock = ADP.IsCatchableExceptionType (e);
                if (decrementAsyncCountOnFailure) {
                    SqlInternalConnectionTds innerConnectionTds = (_activeConnection.InnerConnection as SqlInternalConnectionTds);
                    if (null != innerConnectionTds) { // it may be closed 
                        innerConnectionTds.DecrementAsyncCount();
                    }
                }
                throw;
            }
            finally {
                TdsParser.ReliabilitySection.Assert("unreliable call to RunExecuteReaderTds");  // you need to setup for a thread abort somewhere before you call this method
                if (processFinallyBlock && !async) {
                    // When executing async, we need to keep the _stateObj alive...
                    PutStateObject();
                }
            }

            Debug.Assert(async || null == _stateObj, "non-null state object in RunExecuteReader");
            return ds;
        }

        private SqlDataReader RunExecuteReaderSmi( CommandBehavior cmdBehavior, RunBehavior runBehavior, bool returnStream ) {
            SqlInternalConnectionSmi innerConnection = InternalSmiConnection;

            SmiEventStream eventStream = null;
            SqlDataReader ds = null;
            SmiRequestExecutor requestExecutor = null;
            try {
                // Set it up, process all of the events, and we're done!
                requestExecutor = SetUpSmiRequest( innerConnection );

                long transactionId;
                SysTx.Transaction transaction;
                innerConnection.GetCurrentTransactionPair(out transactionId, out transaction);

                if (Bid.AdvancedOn) {
                    Bid.Trace("<sc.SqlCommand.RunExecuteReaderSmi|ADV> %d#, innerConnection=%d#, transactionId=0x%I64x, commandBehavior=%d.\n", ObjectID, innerConnection.ObjectID, transactionId, (int)cmdBehavior);
                }

                if (SmiContextFactory.Instance.NegotiatedSmiVersion >= SmiContextFactory.KatmaiVersion) {
                    eventStream = requestExecutor.Execute(
                                                    innerConnection.SmiConnection,
                                                    transactionId,
                                                    transaction,
                                                    cmdBehavior,
                                                    SmiExecuteType.Reader
                                                    );
                }
                else {
                    eventStream = requestExecutor.Execute(
                                                    innerConnection.SmiConnection,
                                                    transactionId,
                                                    cmdBehavior,
                                                    SmiExecuteType.Reader
                                                    );
                }

                if ( ( runBehavior & RunBehavior.UntilDone ) != 0 ) {

                    // Consume the results
                    while( eventStream.HasEvents ) {
                        eventStream.ProcessEvent( EventSink );
                    }
                    eventStream.Close( EventSink );
                }

                if ( returnStream ) {
                    ds = new SqlDataReaderSmi( eventStream, this, cmdBehavior, innerConnection, EventSink, requestExecutor );
                    ds.NextResult();    // Position on first set of results
                    _activeConnection.AddWeakReference(ds, SqlReferenceCollection.DataReaderTag);
                }

                EventSink.ProcessMessagesAndThrow();
            }
            catch (Exception e) {
                // VSTS 159716 - we do not want to handle ThreadAbort, OutOfMemory or similar critical exceptions
                // because the state of used objects might remain invalid in this case
                if (!ADP.IsCatchableOrSecurityExceptionType(e)) {
                    throw;
                }

                if (null != eventStream) {
                    eventStream.Close( EventSink );     // 
                }

                if (requestExecutor != null) {
                    requestExecutor.Close(EventSink);
                    EventSink.ProcessMessagesAndThrow(ignoreNonFatalMessages: true);
                }                

                throw;
            }

        return ds;
        }

        private SqlDataReader CompleteAsyncExecuteReader() {
            SqlDataReader ds = cachedAsyncState.CachedAsyncReader; // should not be null
            bool processFinallyBlock = true;
            try {
                FinishExecuteReader(ds, cachedAsyncState.CachedRunBehavior, cachedAsyncState.CachedSetOptions);
            }
            catch (Exception e) {
                processFinallyBlock = ADP.IsCatchableExceptionType(e);
                throw;
            }
            finally {
                TdsParser.ReliabilitySection.Assert("unreliable call to CompleteAsyncExecuteReader");  // you need to setup for a thread abort somewhere before you call this method
                if (processFinallyBlock) {
                    cachedAsyncState.ResetAsyncState();
                    PutStateObject();
                }
            }

            return ds;
        }

        private void FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, string resetOptionsString) {
            // always wrap with a try { FinishExecuteReader(...) } finally { PutStateObject(); }

            NotifyDependency();
            if (runBehavior == RunBehavior.UntilDone) {
                try {
                    bool dataReady;
                    Debug.Assert(_stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
                    bool result = _stateObj.Parser.TryRun(RunBehavior.UntilDone, this, ds, null, _stateObj, out dataReady);
                    if (!result) { throw SQL.SynchronousCallMayNotPend(); }
                }
                catch (Exception e) {
                    // 
                    if (ADP.IsCatchableExceptionType(e)) {
                        if (_inPrepare) {
                            // The flag is expected to be reset by OnReturnValue.  We should receive
                            // the handle unless command execution failed.  If fail, move back to pending
                            // state.
                            _inPrepare = false;                  // reset the flag
                            IsDirty = true;                      // mark command as dirty so it will be prepared next time we're comming through
                            _execType = EXECTYPE.PREPAREPENDING; // reset execution type to pending
                        }

                        if (null != ds) {
                            ds.Close();
                        }
                    }
                    throw;
                }
            }

            // bind the parser to the reader if we get this far
            if (ds != null) {
                ds.Bind(_stateObj);
                _stateObj = null;   // the reader now owns this...
                ds.ResetOptionsString = resetOptionsString;

                // 



                // bind this reader to this connection now
                _activeConnection.AddWeakReference(ds, SqlReferenceCollection.DataReaderTag);

                // force this command to start reading data off the wire.
                // this will cause an error to be reported at Execute() time instead of Read() time
                // if the command is not set.
                try {
                    _cachedMetaData = ds.MetaData;
                    ds.IsInitialized = true; // Webdata 104560
                }
                catch (Exception e) {
                    // 
                    if (ADP.IsCatchableExceptionType(e)) {
                        if (_inPrepare) {
                            // The flag is expected to be reset by OnReturnValue.  We should receive
                            // the handle unless command execution failed.  If fail, move back to pending
                            // state.
                            _inPrepare = false;                  // reset the flag
                            IsDirty = true;                      // mark command as dirty so it will be prepared next time we're comming through
                            _execType = EXECTYPE.PREPAREPENDING; // reset execution type to pending
                        }

                        ds.Close();
                    }

                    throw;
                }
            }
        }

        private void NotifyDependency() {
            if (_sqlDep != null) {
                _sqlDep.StartTimer(Notification);
            }
        }

        public SqlCommand Clone() {
            SqlCommand clone = new SqlCommand(this);
            Bid.Trace("<sc.SqlCommand.Clone|API> %d#, clone=%d#\n", ObjectID, clone.ObjectID);
            return clone;
        }

        object ICloneable.Clone() {
            return Clone();
        }

        private void RegisterForConnectionCloseNotification<T>(ref Task<T> outterTask) {
            SqlConnection connection = _activeConnection;
            if (connection == null) {
                // No connection
                throw ADP.ClosedConnectionError();
            }

            connection.RegisterForConnectionCloseNotification<T>(ref outterTask, this, SqlReferenceCollection.CommandTag);
        }

        // validates that a command has commandText and a non-busy open connection
        // throws exception for error case, returns false if the commandText is empty
        private void ValidateCommand(string method, bool async) {
            if (null == _activeConnection) {
                throw ADP.ConnectionRequired(method);
            }

            // Ensure that the connection is open and that the Parser is in the correct state
            SqlInternalConnectionTds tdsConnection = _activeConnection.InnerConnection as SqlInternalConnectionTds;

            // Ensure that if column encryption override was used then server supports its
            if (((SqlCommandColumnEncryptionSetting.UseConnectionSetting == ColumnEncryptionSetting  && _activeConnection.IsColumnEncryptionSettingEnabled)
                || (ColumnEncryptionSetting == SqlCommandColumnEncryptionSetting.Enabled || ColumnEncryptionSetting == SqlCommandColumnEncryptionSetting.ResultSetOnly))
                   && null != tdsConnection
                   && null != tdsConnection.Parser
                   && !tdsConnection.Parser.IsColumnEncryptionSupported) {
                throw SQL.TceNotSupported ();
            }

            if (tdsConnection != null) {
                var parser = tdsConnection.Parser;
                if ((parser == null) || (parser.State == TdsParserState.Closed)) {
                    throw ADP.OpenConnectionRequired(method, ConnectionState.Closed);
                }
                else if (parser.State != TdsParserState.OpenLoggedIn) {
                    throw ADP.OpenConnectionRequired(method, ConnectionState.Broken);
                }
            }
            else if (_activeConnection.State == ConnectionState.Closed) {
                throw ADP.OpenConnectionRequired(method, ConnectionState.Closed);
            }
            else if (_activeConnection.State == ConnectionState.Broken) {
                throw ADP.OpenConnectionRequired(method, ConnectionState.Broken);
            }

            ValidateAsyncCommand();

            TdsParser bestEffortCleanupTarget = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                {
#endif //DEBUG
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
                    // close any non MARS dead readers, if applicable, and then throw if still busy.
                    // Throw if we have a live reader on this command
                    _activeConnection.ValidateConnectionForExecute(method, this);

                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif //DEBUG
            }
            catch (System.OutOfMemoryException e)
            {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.StackOverflowException e)
            {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.Threading.ThreadAbortException e)
            {
                _activeConnection.Abort(e);
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                throw;
            }
            // Check to see if the currently set transaction has completed.  If so,
            // null out our local reference.
            if (null != _transaction && _transaction.Connection == null)
                _transaction = null;

            // throw if the connection is in a transaction but there is no
            // locally assigned transaction object
            if (_activeConnection.HasLocalTransactionFromAPI && (null == _transaction))
                throw ADP.TransactionRequired(method);

            // if we have a transaction, check to ensure that the active
            // connection property matches the connection associated with
            // the transaction
            if (null != _transaction && _activeConnection != _transaction.Connection)
                throw ADP.TransactionConnectionMismatch();

            if (ADP.IsEmpty(this.CommandText))
                throw ADP.CommandTextRequired(method);

            // Notification property must be null for pre-Yukon connections
            if ((Notification != null) && !_activeConnection.IsYukonOrNewer) {
                throw SQL.NotificationsRequireYukon();
            }

            if ((async) && (_activeConnection.IsContextConnection)) {
                // Async not supported on Context Connections
                throw SQL.NotAvailableOnContextConnection();
            }
        }

        private void ValidateAsyncCommand() {
            // 
            if (cachedAsyncState.PendingAsyncOperation) { // Enforce only one pending async execute at a time.
                if (cachedAsyncState.IsActiveConnectionValid(_activeConnection)) {
                    throw SQL.PendingBeginXXXExists();
                }
                else {
                    _stateObj = null; // Session was re-claimed by session pool upon connection close.
                    cachedAsyncState.ResetAsyncState();
                }
            }
        }

        private void GetStateObject(TdsParser parser = null) {
            Debug.Assert (null == _stateObj,"StateObject not null on GetStateObject");
            Debug.Assert (null != _activeConnection, "no active connection?");

            if (_pendingCancel) {
                _pendingCancel = false; // Not really needed, but we'll reset anyways.

                // If a pendingCancel exists on the object, we must have had a Cancel() call
                // between the point that we entered an Execute* API and the point in Execute* that
                // we proceeded to call this function and obtain a stateObject.  In that case,
                // we now throw a cancelled error.
                throw SQL.OperationCancelled();
            }

            if (parser == null) {
                parser = _activeConnection.Parser;
                if ((parser == null) || (parser.State == TdsParserState.Broken) || (parser.State == TdsParserState.Closed)) {
                    // Connection's parser is null as well, therefore we must be closed
                    throw ADP.ClosedConnectionError();
                }
            }

            TdsParserStateObject stateObj = parser.GetSession(this);            
            stateObj.StartSession(ObjectID);

            _stateObj = stateObj;

            if (_pendingCancel) {
                _pendingCancel = false; // Not really needed, but we'll reset anyways.

                // If a pendingCancel exists on the object, we must have had a Cancel() call
                // between the point that we entered this function and the point where we obtained
                // and actually assigned the stateObject to the local member.  It is possible
                // that the flag is set as well as a call to stateObj.Cancel - though that would
                // be a no-op.  So - throw.
                throw SQL.OperationCancelled();
            }
         }

        private void ReliablePutStateObject() {
            TdsParser bestEffortCleanupTarget = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                {
#endif //DEBUG
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
                    PutStateObject();

                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif //DEBUG
            }
            catch (System.OutOfMemoryException e)
            {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.StackOverflowException e)
            {
                _activeConnection.Abort(e);
                throw;
            }
            catch (System.Threading.ThreadAbortException e)
            {
                _activeConnection.Abort(e);
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                throw;
            }
        }

        private void PutStateObject() {
            TdsParserStateObject stateObj = _stateObj;
            _stateObj = null;

            if (null != stateObj) {
                stateObj.CloseSession();
            }
        }

        /// <summary>
        /// IMPORTANT NOTE: This is created as a copy of OnDoneProc below for Transparent Column Encryption improvement
        /// as there is not much time, to address regressions. Will revisit removing the duplication, when we have time again.
        /// </summary>
        internal void OnDoneDescribeParameterEncryptionProc(TdsParserStateObject stateObj) {
            // called per rpc batch complete
            if (BatchRPCMode) {
                // track the records affected for the just completed rpc batch
                // _rowsAffected is cumulative for ExecuteNonQuery across all rpc batches
                _sqlRPCParameterEncryptionReqArray[_currentlyExecutingDescribeParameterEncryptionRPC].cumulativeRecordsAffected = _rowsAffected;

                _sqlRPCParameterEncryptionReqArray[_currentlyExecutingDescribeParameterEncryptionRPC].recordsAffected =
                    (((0 < _currentlyExecutingDescribeParameterEncryptionRPC) && (0 <= _rowsAffected))
                        ? (_rowsAffected - Math.Max(_sqlRPCParameterEncryptionReqArray[_currentlyExecutingDescribeParameterEncryptionRPC - 1].cumulativeRecordsAffected, 0))
                        : _rowsAffected);

                // track the error collection (not available from TdsParser after ExecuteNonQuery)
                // and the which errors are associated with the just completed rpc batch
                _sqlRPCParameterEncryptionReqArray[_currentlyExecutingDescribeParameterEncryptionRPC].errorsIndexStart =
                    ((0 < _currentlyExecutingDescribeParameterEncryptionRPC)
                        ? _sqlRPCParameterEncryptionReqArray[_currentlyExecutingDescribeParameterEncryptionRPC - 1].errorsIndexEnd
                        : 0);
                _sqlRPCParameterEncryptionReqArray[_currentlyExecutingDescribeParameterEncryptionRPC].errorsIndexEnd = stateObj.ErrorCount;
                _sqlRPCParameterEncryptionReqArray[_currentlyExecutingDescribeParameterEncryptionRPC].errors = stateObj._errors;

                // track the warning collection (not available from TdsParser after ExecuteNonQuery)
                // and the which warnings are associated with the just completed rpc batch
                _sqlRPCParameterEncryptionReqArray[_currentlyExecutingDescribeParameterEncryptionRPC].warningsIndexStart =
                    ((0 < _currentlyExecutingDescribeParameterEncryptionRPC)
                        ? _sqlRPCParameterEncryptionReqArray[_currentlyExecutingDescribeParameterEncryptionRPC - 1].warningsIndexEnd
                        : 0);
                _sqlRPCParameterEncryptionReqArray[_currentlyExecutingDescribeParameterEncryptionRPC].warningsIndexEnd = stateObj.WarningCount;
                _sqlRPCParameterEncryptionReqArray[_currentlyExecutingDescribeParameterEncryptionRPC].warnings = stateObj._warnings;

                _currentlyExecutingDescribeParameterEncryptionRPC++;
            }
        }

        /// <summary>
        /// IMPORTANT NOTE: There is a copy of this function above in OnDoneDescribeParameterEncryptionProc.
        /// Please consider the changes being done in this function for the above function as well.
        /// </summary>
        internal void OnDoneProc() { // called per rpc batch complete
            if (BatchRPCMode) {

                // track the records affected for the just completed rpc batch
                // _rowsAffected is cumulative for ExecuteNonQuery across all rpc batches
                _SqlRPCBatchArray[_currentlyExecutingBatch].cumulativeRecordsAffected = _rowsAffected;

                _SqlRPCBatchArray[_currentlyExecutingBatch].recordsAffected =
                    (((0 < _currentlyExecutingBatch) && (0 <= _rowsAffected))
                        ? (_rowsAffected - Math.Max(_SqlRPCBatchArray[_currentlyExecutingBatch-1].cumulativeRecordsAffected, 0))
                        : _rowsAffected);

                // track the error collection (not available from TdsParser after ExecuteNonQuery)
                // and the which errors are associated with the just completed rpc batch
                _SqlRPCBatchArray[_currentlyExecutingBatch].errorsIndexStart =
                    ((0 < _currentlyExecutingBatch)
                        ? _SqlRPCBatchArray[_currentlyExecutingBatch-1].errorsIndexEnd
                        : 0);
                _SqlRPCBatchArray[_currentlyExecutingBatch].errorsIndexEnd = _stateObj.ErrorCount;
                _SqlRPCBatchArray[_currentlyExecutingBatch].errors = _stateObj._errors;
                
                // track the warning collection (not available from TdsParser after ExecuteNonQuery)
                // and the which warnings are associated with the just completed rpc batch
                _SqlRPCBatchArray[_currentlyExecutingBatch].warningsIndexStart =
                    ((0 < _currentlyExecutingBatch)
                        ? _SqlRPCBatchArray[_currentlyExecutingBatch-1].warningsIndexEnd
                        : 0);
                _SqlRPCBatchArray[_currentlyExecutingBatch].warningsIndexEnd = _stateObj.WarningCount;
                _SqlRPCBatchArray[_currentlyExecutingBatch].warnings = _stateObj._warnings;

                _currentlyExecutingBatch++;
                Debug.Assert(_parameterCollectionList.Count >= _currentlyExecutingBatch, "OnDoneProc: Too many DONEPROC events");
            }
        }

        //
        // 


        internal void OnReturnStatus(int status) {
            if (_inPrepare)
                return;

            // Don't set the return status if this is the status for sp_describe_parameter_encryption.
            if (IsDescribeParameterEncryptionRPCCurrentlyInProgress)
                return;

            SqlParameterCollection parameters = _parameters;
            if (BatchRPCMode) {
                if (_parameterCollectionList.Count > _currentlyExecutingBatch) {
                    parameters = _parameterCollectionList[_currentlyExecutingBatch];
                }
                else {
                    Debug.Assert(false, "OnReturnStatus: SqlCommand got too many DONEPROC events");
                    parameters = null;
                }
            }
            // see if a return value is bound
            int count = GetParameterCount(parameters);
            for (int i = 0; i < count; i++) {
                SqlParameter parameter = parameters[i];
                if (parameter.Direction == ParameterDirection.ReturnValue) {
                    object v = parameter.Value;

                // if the user bound a sqlint32 (the only valid one for status, use it)
                if ( (null != v) && (v.GetType() == typeof(SqlInt32)) ) {
                        parameter.Value = new SqlInt32(status); // value type
                }
                else {
                        parameter.Value = status;

                    }
                    break;
                }
            }
        }

        //
        // Move the return value to the corresponding output parameter.
        // Return parameters are sent in the order in which they were defined in the procedure.
        // If named, match the parameter name, otherwise fill in based on ordinal position.
        // If the parameter is not bound, then ignore the return value.
        //
        internal void OnReturnValue(SqlReturnValue rec, TdsParserStateObject stateObj) {

            if (_inPrepare) {
                if (!rec.value.IsNull) {
                    _prepareHandle = rec.value.Int32;
                }
                _inPrepare = false;
                return;
            }

            SqlParameterCollection parameters = GetCurrentParameterCollection();
            int  count      = GetParameterCount(parameters);


            SqlParameter thisParam = GetParameterForOutputValueExtraction(parameters, rec.parameter, count);

            if (null != thisParam) {
                // If the parameter's direction is InputOutput, Output or ReturnValue and it needs to be transparently encrypted/decrypted
                // then simply decrypt, deserialize and set the value.
                if (rec.cipherMD != null &&
                    thisParam.CipherMetadata != null && 
                    (thisParam.Direction == ParameterDirection.Output || 
                    thisParam.Direction == ParameterDirection.InputOutput || 
                    thisParam.Direction == ParameterDirection.ReturnValue)) {
                    if(rec.tdsType != TdsEnums.SQLBIGVARBINARY) {
                        throw SQL.InvalidDataTypeForEncryptedParameter(thisParam.ParameterNameFixed, rec.tdsType, TdsEnums.SQLBIGVARBINARY);
                    }

                    // Decrypt the ciphertext
                    TdsParser parser = _activeConnection.Parser;
                    if ((parser == null) || (parser.State == TdsParserState.Closed) || (parser.State == TdsParserState.Broken)) {
                        throw ADP.ClosedConnectionError();
                    }

                    if (!rec.value.IsNull) {
                        try {
                            Debug.Assert(_activeConnection != null, @"_activeConnection should not be null");

                            // Get the key information from the parameter and decrypt the value.
                            rec.cipherMD.EncryptionInfo = thisParam.CipherMetadata.EncryptionInfo;
                            byte[] unencryptedBytes = SqlSecurityUtility.DecryptWithKey(rec.value.ByteArray, rec.cipherMD, _activeConnection.DataSource);

                            if (unencryptedBytes != null) {
                                // Denormalize the value and convert it to the parameter type.
                                SqlBuffer buffer = new SqlBuffer();
                                parser.DeserializeUnencryptedValue(buffer, unencryptedBytes, rec, stateObj, rec.NormalizationRuleVersion);
                                thisParam.SetSqlBuffer(buffer);
                            }
                        }
                        catch (Exception e) {
                            throw SQL.ParamDecryptionFailed(thisParam.ParameterNameFixed, null, e);
                        }
                    }
                    else {
                        // Create a new SqlBuffer and set it to null
                        // Note: We can't reuse the SqlBuffer in "rec" below since it's already been set (to varbinary)
                        // in previous call to TryProcessReturnValue(). 
                        // Note 2: We will be coming down this code path only if the Command Setting is set to use TCE.
                        // We pass the command setting as TCE enabled in the below call for this reason.
                        SqlBuffer buff = new SqlBuffer();
                        TdsParser.GetNullSqlValue(buff, rec, SqlCommandColumnEncryptionSetting.Enabled, parser.Connection);
                        thisParam.SetSqlBuffer(buff);
                    }
                }
                else {
                    // copy over data

                    // if the value user has supplied a SqlType class, then just copy over the SqlType, otherwise convert
                    // to the com type
                    object val = thisParam.Value;

                    //set the UDT value as typed object rather than bytes
                    if (SqlDbType.Udt == thisParam.SqlDbType) {
                        object data = null;
                        try {
                            Connection.CheckGetExtendedUDTInfo(rec, true);

                            //extract the byte array from the param value
                            if (rec.value.IsNull)
                                data = DBNull.Value;
                            else {
                                data = rec.value.ByteArray; //should work for both sql and non-sql values
                            }

                            //call the connection to instantiate the UDT object
                            thisParam.Value = Connection.GetUdtValue(data, rec, false);
                        }
                        catch (FileNotFoundException e) {
                            // SQL BU DT 329981
                            // Assign Assembly.Load failure in case where assembly not on client.
                            // This allows execution to complete and failure on SqlParameter.Value.
                            thisParam.SetUdtLoadError(e);
                        }
                        catch (FileLoadException e) {
                            // SQL BU DT 329981
                            // Assign Assembly.Load failure in case where assembly cannot be loaded on client.
                            // This allows execution to complete and failure on SqlParameter.Value.
                            thisParam.SetUdtLoadError(e);
                        }

                        return;
                    } else {
                        thisParam.SetSqlBuffer(rec.value);
                    }

                    MetaType mt = MetaType.GetMetaTypeFromSqlDbType(rec.type, rec.isMultiValued);

                    if (rec.type == SqlDbType.Decimal) {
                        thisParam.ScaleInternal = rec.scale;
                        thisParam.PrecisionInternal = rec.precision;
                    }
                    else if (mt.IsVarTime) {
                        thisParam.ScaleInternal = rec.scale;
                    }
                    else if (rec.type == SqlDbType.Xml) {
                        SqlCachedBuffer cachedBuffer = (thisParam.Value as SqlCachedBuffer);
                        if (null != cachedBuffer) {
                            thisParam.Value = cachedBuffer.ToString();
                        }
                    }

                    if (rec.collation != null) {
                        Debug.Assert(mt.IsCharType, "Invalid collation structure for non-char type");
                        thisParam.Collation = rec.collation;
                    }
                }
            }

            return;
        }

        internal void OnParametersAvailableSmi( SmiParameterMetaData[] paramMetaData, ITypedGettersV3 parameterValues ) {
            Debug.Assert(null != paramMetaData);

            for(int index=0; index < paramMetaData.Length; index++) {
                OnParameterAvailableSmi(paramMetaData[index], parameterValues, index);
            }
        }

        internal void OnParameterAvailableSmi(SmiParameterMetaData metaData, ITypedGettersV3 parameterValues, int ordinal) {
            if ( ParameterDirection.Input != metaData.Direction ) {
                string name = null;
                if (ParameterDirection.ReturnValue != metaData.Direction) {
                    name = metaData.Name;
                }

                SqlParameterCollection parameters = GetCurrentParameterCollection();
                int  count      = GetParameterCount(parameters);
                SqlParameter param = GetParameterForOutputValueExtraction(parameters, name, count);

                if ( null != param ) {
                    param.LocaleId = (int)metaData.LocaleId;
                    param.CompareInfo = metaData.CompareOptions;
                    SqlBuffer buffer = new SqlBuffer();
                    object result;
                    if (_activeConnection.IsKatmaiOrNewer) {
                        result = ValueUtilsSmi.GetOutputParameterV200Smi(
                                OutParamEventSink, (SmiTypedGetterSetter)parameterValues, ordinal, metaData, _smiRequestContext, buffer );
                    }
                    else {
                        result = ValueUtilsSmi.GetOutputParameterV3Smi( 
                                    OutParamEventSink, parameterValues, ordinal, metaData, _smiRequestContext, buffer );
                    }
                    if ( null != result ) {
                        param.Value = result;
                    }
                    else {
                        param.SetSqlBuffer( buffer );
                    }
                }
            }
        }

        private SqlParameterCollection GetCurrentParameterCollection() {
            if (BatchRPCMode) {
                if (_parameterCollectionList.Count > _currentlyExecutingBatch) {
                    return _parameterCollectionList[_currentlyExecutingBatch];
                }
                else {
                    Debug.Assert(false, "OnReturnValue: SqlCommand got too many DONEPROC events");
                    return null;
                }
            }
            else {
                return _parameters;
            }
        }

        private SqlParameter GetParameterForOutputValueExtraction( SqlParameterCollection parameters,
                        string paramName, int paramCount ) {
            SqlParameter thisParam = null;
            bool foundParam = false;

            if (null == paramName) {
                // rec.parameter should only be null for a return value from a function
                for (int i = 0; i < paramCount; i++) {
                    thisParam = parameters[i];
                    // searching for ReturnValue
                    if (thisParam.Direction == ParameterDirection.ReturnValue) {
                                foundParam = true;
                            break; // found it
                    }
                }
            }
            else {
                for (int i = 0; i < paramCount; i++) {
                    thisParam = parameters[i];
                    // searching for Output or InputOutput or ReturnValue with matching name
                    if (thisParam.Direction != ParameterDirection.Input && thisParam.Direction != ParameterDirection.ReturnValue  && paramName == thisParam.ParameterNameFixed) {
                                foundParam = true;
                            break; // found it
                        }
                    }
            }
            if (foundParam)
                return thisParam;
            else
                return null;
        }

        private void GetRPCObject(int paramCount, ref _SqlRPC rpc, bool forSpDescribeParameterEncryption = false) {
            // Designed to minimize necessary allocations
            int ii;
            if (rpc == null) {
                if (!forSpDescribeParameterEncryption) {
                    if (_rpcArrayOf1 == null) {
                        _rpcArrayOf1 = new _SqlRPC[1];
                        _rpcArrayOf1[0] = new _SqlRPC();
                    }

                    rpc = _rpcArrayOf1[0];
                }
                else {
                    if (_rpcForEncryption == null) {
                        _rpcForEncryption = new _SqlRPC();
                    }

                    rpc = _rpcForEncryption;
                }
            }

            rpc.ProcID = 0;
            rpc.rpcName = null;
            rpc.options = 0;

            rpc.recordsAffected = default(int?);
            rpc.cumulativeRecordsAffected = -1;

            rpc.errorsIndexStart = 0;
            rpc.errorsIndexEnd = 0;
            rpc.errors = null;
            
            rpc.warningsIndexStart = 0;
            rpc.warningsIndexEnd = 0;
            rpc.warnings = null;
            rpc.needsFetchParameterEncryptionMetadata = false;

            // Make sure there is enough space in the parameters and paramoptions arrays
            if(rpc.parameters == null || rpc.parameters.Length < paramCount) {
                rpc.parameters = new SqlParameter[paramCount];
            }
            else if (rpc.parameters.Length > paramCount) {
                        rpc.parameters[paramCount]=null;    // Terminator
            }
            if(rpc.paramoptions == null || (rpc.paramoptions.Length < paramCount)) {
                rpc.paramoptions = new byte[paramCount];
            }
            else {
                for (ii = 0 ; ii < paramCount ; ii++)
                    rpc.paramoptions[ii] = 0;
            }
        }

        private void SetUpRPCParameters (_SqlRPC rpc, int startCount, bool inSchema, SqlParameterCollection parameters) {
            int ii;
            int paramCount = GetParameterCount(parameters) ;
            int j = startCount;
            TdsParser parser = _activeConnection.Parser;
            bool yukonOrNewer = parser.IsYukonOrNewer;

            for (ii = 0;  ii < paramCount; ii++) {
                SqlParameter parameter = parameters[ii];
                parameter.Validate(ii, CommandType.StoredProcedure == CommandType);

                // func will change type to that with a 4 byte length if the type has a two
                // byte length and a parameter length > than that expressable in 2 bytes
                if ((!parameter.ValidateTypeLengths(yukonOrNewer).IsPlp) && (parameter.Direction != ParameterDirection.Output)) {
                    parameter.FixStreamDataForNonPLP();
                }

                if (ShouldSendParameter(parameter)) {
                    rpc.parameters[j] = parameter;

                    // set output bit
                    if (parameter.Direction == ParameterDirection.InputOutput ||
                        parameter.Direction == ParameterDirection.Output)
                        rpc.paramoptions[j] = TdsEnums.RPC_PARAM_BYREF;

                    // Set the encryped bit, if the parameter is to be encrypted.
                    if (parameter.CipherMetadata != null) {
                        rpc.paramoptions[j] |= TdsEnums.RPC_PARAM_ENCRYPTED;
                    }

                    // set default value bit
                    if (parameter.Direction != ParameterDirection.Output) {
                        // remember that null == Convert.IsEmpty, DBNull.Value is a database null!

                        // MDAC 62117, don't assume a default value exists for parameters in the case when
                        // the user is simply requesting schema
                        // SQLBUVSTS 179488 TVPs use DEFAULT and do not allow NULL, even for schema only.
                        if (null == parameter.Value && (!inSchema || SqlDbType.Structured == parameter.SqlDbType)) {
                            rpc.paramoptions[j] |= TdsEnums.RPC_PARAM_DEFAULT;
                        }
                    }

                    // Must set parameter option bit for LOB_COOKIE if unfilled LazyMat blob
                    j++;
                }
            }

        }

        //
        // 7.5
        // prototype for sp_prepexec is:
        // sp_prepexec(@handle int IN/OUT, @batch_params ntext, @batch_text ntext, param1value,param2value...)
        //
        private _SqlRPC  BuildPrepExec(CommandBehavior behavior) {
            Debug.Assert(System.Data.CommandType.Text == this.CommandType, "invalid use of sp_prepexec for stored proc invocation!");
            SqlParameter sqlParam;
            int j = 3;

            int count = CountSendableParameters(_parameters);

            _SqlRPC rpc = null;
            GetRPCObject(count + j, ref rpc);

            rpc.ProcID = TdsEnums.RPC_PROCID_PREPEXEC;
            rpc.rpcName = TdsEnums.SP_PREPEXEC;

            //@handle
            sqlParam = new SqlParameter(null, SqlDbType.Int);
            sqlParam.Direction = ParameterDirection.InputOutput;
            sqlParam.Value = _prepareHandle;
            rpc.parameters[0] = sqlParam;
            rpc.paramoptions[0] = TdsEnums.RPC_PARAM_BYREF;

            //@batch_params
            string paramList = BuildParamList(_stateObj.Parser, _parameters);
            sqlParam = new SqlParameter(null, ((paramList.Length<<1)<=TdsEnums.TYPE_SIZE_LIMIT)?SqlDbType.NVarChar:SqlDbType.NText, paramList.Length);
            sqlParam.Value = paramList;
            rpc.parameters[1] = sqlParam;

            //@batch_text
            string text = GetCommandText(behavior);
            sqlParam = new SqlParameter(null, ((text.Length<<1)<=TdsEnums.TYPE_SIZE_LIMIT)?SqlDbType.NVarChar:SqlDbType.NText, text.Length);
            sqlParam.Value = text;
            rpc.parameters[2] = sqlParam;

            SetUpRPCParameters (rpc,  j, false, _parameters);
            return rpc;
        }


        //
        // returns true if the parameter is not a return value
        // and it's value is not DBNull (for a nullable parameter)
        //
        private static bool ShouldSendParameter(SqlParameter p, bool includeReturnValue = false) {
            switch (p.Direction) {
            case ParameterDirection.ReturnValue:
                // return value parameters are not sent, except for the parameter list of sp_describe_parameter_encryption
                return includeReturnValue;
            case ParameterDirection.Output:
            case ParameterDirection.InputOutput:
            case ParameterDirection.Input:
                // InputOutput/Output parameters are aways sent
                return true;
            default:
                Debug.Assert(false, "Invalid ParameterDirection!");
                return false;
            }
        }

        private int CountSendableParameters(SqlParameterCollection parameters) {
            int cParams = 0;

            if (parameters != null) {
                int count = parameters.Count;
                for (int i = 0; i < count; i++) {
                    if (ShouldSendParameter(parameters[i]))
                        cParams++;
                }
            }
            return cParams;
        }

        // Returns total number of parameters
        private int GetParameterCount(SqlParameterCollection parameters) {
            return ((null != parameters) ? parameters.Count : 0);
        }

        //
        // build the RPC record header for this stored proc and add parameters
        //
        private void BuildRPC(bool inSchema, SqlParameterCollection parameters, ref _SqlRPC rpc) {
            Debug.Assert(this.CommandType == System.Data.CommandType.StoredProcedure, "Command must be a stored proc to execute an RPC");
            int count = CountSendableParameters(parameters);
            GetRPCObject(count, ref rpc);

            rpc.rpcName = this.CommandText; // just get the raw command text

            SetUpRPCParameters ( rpc, 0, inSchema, parameters);
        }

        //
        // build the RPC record header for sp_unprepare
        //
        // prototype for sp_unprepare is:
        // sp_unprepare(@handle)
        //
        // 
        private _SqlRPC BuildUnprepare() {
            Debug.Assert(_prepareHandle != 0, "Invalid call to sp_unprepare without a valid handle!");

            _SqlRPC rpc = null;
            GetRPCObject(1, ref rpc);
            SqlParameter sqlParam;

            rpc.ProcID = TdsEnums.RPC_PROCID_UNPREPARE;
            rpc.rpcName = TdsEnums.SP_UNPREPARE;

            //@handle
            sqlParam = new SqlParameter(null, SqlDbType.Int);
            sqlParam.Value = _prepareHandle;
            rpc.parameters[0] = sqlParam;

            return rpc;
        }

        //
        // build the RPC record header for sp_execute
        //
        // prototype for sp_execute is:
        // sp_execute(@handle int,param1value,param2value...)
        //
        private _SqlRPC BuildExecute(bool inSchema) {
            Debug.Assert(_prepareHandle != -1, "Invalid call to sp_execute without a valid handle!");
            int j = 1;

            int count = CountSendableParameters(_parameters);

            _SqlRPC rpc = null;
            GetRPCObject(count + j, ref rpc);

            SqlParameter sqlParam;

            rpc.ProcID = TdsEnums.RPC_PROCID_EXECUTE;
            rpc.rpcName = TdsEnums.SP_EXECUTE;

            //@handle
            sqlParam = new SqlParameter(null, SqlDbType.Int);
            sqlParam.Value = _prepareHandle;
            rpc.parameters[0] = sqlParam;

            SetUpRPCParameters (rpc, j, inSchema, _parameters);
            return rpc;
        }

        //
        // build the RPC record header for sp_executesql and add the parameters
        //
        // prototype for sp_executesql is:
        // sp_executesql(@batch_text nvarchar(4000),@batch_params nvarchar(4000), param1,.. paramN)
        private void BuildExecuteSql(CommandBehavior behavior, string commandText, SqlParameterCollection parameters, ref _SqlRPC rpc) {

            Debug.Assert(_prepareHandle == -1, "This command has an existing handle, use sp_execute!");
            Debug.Assert(System.Data.CommandType.Text == this.CommandType, "invalid use of sp_executesql for stored proc invocation!");
            int j;
            SqlParameter sqlParam;

            int cParams = CountSendableParameters(parameters);
            if (cParams > 0) {
                j = 2;
            }
            else {
                j =1;
            }

            GetRPCObject(cParams + j, ref rpc);
            rpc.ProcID = TdsEnums.RPC_PROCID_EXECUTESQL;
            rpc.rpcName = TdsEnums.SP_EXECUTESQL;

            // @sql
            if (commandText == null) {
                commandText = GetCommandText(behavior);
            }
            sqlParam = new SqlParameter(null, ((commandText.Length<<1)<=TdsEnums.TYPE_SIZE_LIMIT)?SqlDbType.NVarChar:SqlDbType.NText, commandText.Length);
            sqlParam.Value = commandText;
            rpc.parameters[0] = sqlParam;

            if (cParams > 0) {
                string paramList = BuildParamList(_stateObj.Parser, BatchRPCMode  ? parameters : _parameters);
                sqlParam = new SqlParameter(null, ((paramList.Length<<1)<=TdsEnums.TYPE_SIZE_LIMIT)?SqlDbType.NVarChar:SqlDbType.NText, paramList.Length);
                sqlParam.Value = paramList;
                rpc.parameters[1] = sqlParam;

                bool inSchema =  (0 != (behavior & CommandBehavior.SchemaOnly));
                SetUpRPCParameters (rpc, j,  inSchema, parameters);
            }
        }

        /// <summary>
        /// This function constructs a string parameter containing the exec statement in the following format
        /// N'EXEC sp_name @param1=@param1, @param1=@param2, ..., @paramN=@paramN'
        /// 




        private SqlParameter BuildStoredProcedureStatementForColumnEncryption(string storedProcedureName, SqlParameter[] parameters) {
            Debug.Assert(CommandType == CommandType.StoredProcedure, "BuildStoredProcedureStatementForColumnEncryption() should only be called for stored procedures");
            Debug.Assert(!string.IsNullOrWhiteSpace(storedProcedureName), "storedProcedureName cannot be null or empty in BuildStoredProcedureStatementForColumnEncryption");
            Debug.Assert(parameters != null, "parameters cannot be null in BuildStoredProcedureStatementForColumnEncryption");

            StringBuilder execStatement = new StringBuilder();
            execStatement.Append(@"EXEC ");

            // Find the return value parameter (if any).
            SqlParameter returnValueParameter = null;
            foreach (SqlParameter parameter in parameters) {
                if (parameter.Direction == ParameterDirection.ReturnValue) {
                    returnValueParameter = parameter;
                    break;
                }
            }

            // If there is a return value parameter we need to assign the result to it.
            // EXEC @returnValue = moduleName [parameters]
            if (returnValueParameter != null) {
                execStatement.AppendFormat(@"{0}=", returnValueParameter.ParameterNameFixed);
            }

            execStatement.Append(ParseAndQuoteIdentifier(storedProcedureName, false));

            // Build parameter list in the format
            // @param1=@param1, @param1=@param2, ..., @paramn=@paramn

            // Append the first parameter
            int i = 0;

            if(parameters.Count() > 0) {
                // Skip the return value parameters.
                while (i < parameters.Count() && parameters[i].Direction == ParameterDirection.ReturnValue) {
                    i++;
                }

                if (i < parameters.Count()) {
                    // Possibility of a SQL Injection issue through parameter names and how to construct valid identifier for parameters.
                    // Since the parameters comes from application itself, there should not be a security vulnerability.
                    // Also since the query is not executed, but only analyzed there is no possibility for elevation of priviledge, but only for 
                    // incorrect results which would only affect the user that attempts the injection.
                    execStatement.AppendFormat(@" {0}={0}", parameters[i].ParameterNameFixed);
                
                    // InputOutput and Output parameters need to be marked as such.
                    if (parameters[i].Direction == ParameterDirection.Output ||
                        parameters[i].Direction == ParameterDirection.InputOutput) {
                        execStatement.AppendFormat(@" OUTPUT");
                    }
                }
            }

            // Move to the next parameter.
            i++;

            // Append the rest of parameters
            for (; i < parameters.Count(); i++) {
                if (parameters[i].Direction != ParameterDirection.ReturnValue) {
                    execStatement.AppendFormat(@", {0}={0}", parameters[i].ParameterNameFixed);

                    // InputOutput and Output parameters need to be marked as such.
                    if (parameters[i].Direction == ParameterDirection.Output ||
                    parameters[i].Direction == ParameterDirection.InputOutput) {
                        execStatement.AppendFormat(@" OUTPUT");
                    }
                }
            }

            // Construct @tsql SqlParameter to be returned
            SqlParameter tsqlParameter = new SqlParameter(null, ((execStatement.Length << 1) <= TdsEnums.TYPE_SIZE_LIMIT) ? SqlDbType.NVarChar : SqlDbType.NText, execStatement.Length);
            tsqlParameter.Value = execStatement.ToString();

            return tsqlParameter;
        }

        // paramList parameter for sp_executesql, sp_prepare, and sp_prepexec
        internal string BuildParamList(TdsParser parser, SqlParameterCollection parameters, bool includeReturnValue = false) {
            StringBuilder paramList = new StringBuilder();
            bool fAddSeperator = false;

            bool yukonOrNewer = parser.IsYukonOrNewer;

            int count = 0;

            count = parameters.Count;
            for (int i = 0; i < count; i++) {
                SqlParameter sqlParam = parameters[i];
                sqlParam.Validate(i, CommandType.StoredProcedure == CommandType);
                // skip ReturnValue parameters; we never send them to the server
                if (!ShouldSendParameter(sqlParam, includeReturnValue))
                    continue;

                // add our separator for the ith parmeter
                if (fAddSeperator)
                    paramList.Append(',');

                paramList.Append(sqlParam.ParameterNameFixed);

                MetaType mt = sqlParam.InternalMetaType;

                //for UDTs, get the actual type name. Get only the typename, omitt catalog and schema names.
                //in TSQL you should only specify the unqualified type name

                // paragraph above doesn't seem to be correct. Server won't find the type
                // if we don't provide a fully qualified name
                paramList.Append(" ");
                if (mt.SqlDbType == SqlDbType.Udt) {
                    string fullTypeName = sqlParam.UdtTypeName;
                    if(ADP.IsEmpty(fullTypeName))
                        throw SQL.MustSetUdtTypeNameForUdtParams();
                    // DEVNOTE: do we need to escape the full type name?
                    paramList.Append(ParseAndQuoteIdentifier(fullTypeName, true /* is UdtTypeName */));
                }
                else if (mt.SqlDbType == SqlDbType.Structured) {
                    string typeName = sqlParam.TypeName;
                    if (ADP.IsEmpty(typeName)) {
                        throw SQL.MustSetTypeNameForParam(mt.TypeName, sqlParam.ParameterNameFixed);
                    }
                    paramList.Append(ParseAndQuoteIdentifier(typeName, false /* is not UdtTypeName*/));

                    // TVPs currently are the only Structured type and must be read only, so add that keyword
                    paramList.Append(" READONLY");
                }
                else {
                    // func will change type to that with a 4 byte length if the type has a two
                    // byte length and a parameter length > than that expressable in 2 bytes
                    mt  = sqlParam.ValidateTypeLengths(yukonOrNewer);
                    if ((!mt.IsPlp) && (sqlParam.Direction != ParameterDirection.Output)) {
                        sqlParam.FixStreamDataForNonPLP();
                    }
                    paramList.Append(mt.TypeName);
                }

                fAddSeperator = true;

                if (mt.SqlDbType == SqlDbType.Decimal) {
                    byte precision = sqlParam.GetActualPrecision();
                    byte scale = sqlParam.GetActualScale();

                    paramList.Append('(');

                    if (0 == precision) {
                        if (IsShiloh) {
                            precision = TdsEnums.DEFAULT_NUMERIC_PRECISION;
                        } else {
                            precision = TdsEnums.SPHINX_DEFAULT_NUMERIC_PRECISION;
                        }
                    }

                    paramList.Append(precision);
                    paramList.Append(',');
                    paramList.Append(scale);
                    paramList.Append(')');
                }
                else if (mt.IsVarTime) {
                    byte scale = sqlParam.GetActualScale();

                    paramList.Append('(');
                    paramList.Append(scale);
                    paramList.Append(')');
                }
                else if (false == mt.IsFixed && false == mt.IsLong && mt.SqlDbType != SqlDbType.Timestamp && mt.SqlDbType != SqlDbType.Udt && SqlDbType.Structured != mt.SqlDbType) {
                    int size = sqlParam.Size;

                    paramList.Append('(');

                    // if using non unicode types, obtain the actual byte length from the parser, with it's associated code page
                    if (mt.IsAnsiType) {
                        object val = sqlParam.GetCoercedValue();
                        string s = null;

                        // deal with the sql types
                        if ((null != val) && (DBNull.Value != val)) {
                            s = (val as string);
                            if (null == s) {
                                SqlString sval = val is SqlString ? (SqlString)val : SqlString.Null;
                                if (!sval.IsNull) {
                                    s = sval.Value;
                                }
                            }
                        }

                        if (null != s) {
                            int actualBytes = parser.GetEncodingCharLength(s, sqlParam.GetActualSize(), sqlParam.Offset, null);
                            // if actual number of bytes is greater than the user given number of chars, use actual bytes
                            if (actualBytes > size)
                                size = actualBytes;
                        }
                    }

                    // 

                    if (0 == size)
                        size = mt.IsSizeInCharacters ? (TdsEnums.MAXSIZE >> 1) : TdsEnums.MAXSIZE;

                    paramList.Append(size);
                    paramList.Append(')');
                }
                else if (mt.IsPlp && (mt.SqlDbType != SqlDbType.Xml) && (mt.SqlDbType != SqlDbType.Udt)) {
                    paramList.Append("(max) ");
                }

                // set the output bit for Output or InputOutput parameters
                if (sqlParam.Direction != ParameterDirection.Input)
                    paramList.Append(" " + TdsEnums.PARAM_OUTPUT);
            }

            return paramList.ToString();
        }

        // Adds quotes to each part of a SQL identifier that may be multi-part, while leaving
        //  the result as a single composite name.
        private string ParseAndQuoteIdentifier(string identifier, bool isUdtTypeName) {
            string[] strings = SqlParameter.ParseTypeName(identifier, isUdtTypeName);
            StringBuilder bld = new StringBuilder();

            // Stitching back together is a little tricky. Assume we want to build a full multi-part name
            //  with all parts except trimming separators for leading empty names (null or empty strings,
            //  but not whitespace). Separators in the middle should be added, even if the name part is 
            //  null/empty, to maintain proper location of the parts.
            for (int i = 0; i < strings.Length; i++ ) {
                if (0 < bld.Length) {
                    bld.Append('.');
                }
                if (null != strings[i] && 0 != strings[i].Length) {
                    bld.Append(ADP.BuildQuotedString("[", "]", strings[i]));
                }
            }

            return bld.ToString();
        }

        // returns set option text to turn on format only and key info on and off
        // @devnote:  When we are executing as a text command, then we never need
        // to turn off the options since they command text is executed in the scope of sp_executesql.
        // For a stored proc command, however, we must send over batch sql and then turn off
        // the set options after we read the data.  See the code in Command.Execute()
        private string GetSetOptionsString(CommandBehavior behavior) {
            string s = null;

            if ((System.Data.CommandBehavior.SchemaOnly == (behavior & CommandBehavior.SchemaOnly)) ||
               (System.Data.CommandBehavior.KeyInfo == (behavior & CommandBehavior.KeyInfo))) {

                // MDAC 56898 - SET FMTONLY ON will cause the server to ignore other SET OPTIONS, so turn
                // it off before we ask for browse mode metadata
                s = TdsEnums.FMTONLY_OFF;

                if (System.Data.CommandBehavior.KeyInfo == (behavior & CommandBehavior.KeyInfo)) {
                    s = s + TdsEnums.BROWSE_ON;
                }

                if (System.Data.CommandBehavior.SchemaOnly == (behavior & CommandBehavior.SchemaOnly)) {
                    s = s + TdsEnums.FMTONLY_ON;
                }
            }

            return s;
        }

        private string GetResetOptionsString(CommandBehavior behavior) {
            string s = null;

            // SET FMTONLY ON OFF
            if (System.Data.CommandBehavior.SchemaOnly == (behavior & CommandBehavior.SchemaOnly)) {
                s = s + TdsEnums.FMTONLY_OFF;
            }

            // SET NO_BROWSETABLE OFF
            if (System.Data.CommandBehavior.KeyInfo == (behavior & CommandBehavior.KeyInfo)) {
                s = s + TdsEnums.BROWSE_OFF;
            }

            return s;
        }

        private String GetCommandText(CommandBehavior behavior) {
            // build the batch string we send over, since we execute within a stored proc (sp_executesql), the SET options never need to be
            // turned off since they are scoped to the sproc
            Debug.Assert(System.Data.CommandType.Text == this.CommandType, "invalid call to GetCommandText for stored proc!");
            return GetSetOptionsString(behavior) + this.CommandText;
        }

        //
        // build the RPC record header for sp_executesql and add the parameters
        //
        // the prototype for sp_prepare is:
        // sp_prepare(@handle int OUTPUT, @batch_params ntext, @batch_text ntext, @options int default 0x1)
        private _SqlRPC BuildPrepare(CommandBehavior behavior) {
            Debug.Assert(System.Data.CommandType.Text == this.CommandType, "invalid use of sp_prepare for stored proc invocation!");

            _SqlRPC rpc = null;
            GetRPCObject(3, ref rpc);
            SqlParameter sqlParam;

            rpc.ProcID = TdsEnums.RPC_PROCID_PREPARE;
            rpc.rpcName = TdsEnums.SP_PREPARE;

            //@handle
            sqlParam = new SqlParameter(null, SqlDbType.Int);
            sqlParam.Direction = ParameterDirection.Output;
            rpc.parameters[0] = sqlParam;
            rpc.paramoptions[0] = TdsEnums.RPC_PARAM_BYREF;

            //@batch_params
            string paramList = BuildParamList(_stateObj.Parser, _parameters);
            sqlParam = new SqlParameter(null, ((paramList.Length<<1)<=TdsEnums.TYPE_SIZE_LIMIT)?SqlDbType.NVarChar:SqlDbType.NText, paramList.Length);
            sqlParam.Value = paramList;
            rpc.parameters[1] = sqlParam;

            //@batch_text
            string text = GetCommandText(behavior);
            sqlParam = new SqlParameter(null, ((text.Length<<1)<=TdsEnums.TYPE_SIZE_LIMIT)?SqlDbType.NVarChar:SqlDbType.NText, text.Length);
            sqlParam.Value = text;
            rpc.parameters[2] = sqlParam;

/*
            //@options
            sqlParam = new SqlParameter(null, SqlDbType.Int);
            rpc.Parameters[3] = sqlParam;
*/
            return rpc;
        }

        internal void CheckThrowSNIException() {
            var stateObj = _stateObj;
            if (stateObj != null) {
                stateObj.CheckThrowSNIException();
            }
        }

        // We're being notified that the underlying connection has closed
        internal void OnConnectionClosed() {
            
            var stateObj = _stateObj;
            if (stateObj != null) {
                stateObj.OnConnectionClosed();
            }
        }


        internal TdsParserStateObject StateObject {
            get {
                return _stateObj;
            }
        }

        private bool IsPrepared {
            get { return(_execType != EXECTYPE.UNPREPARED);}
        }

        private bool IsUserPrepared {
            get { return IsPrepared && !_hiddenPrepare && !IsDirty; }
        }

        internal bool IsDirty {
            get {
                // only dirty if prepared
                var activeConnection = _activeConnection;
                return (IsPrepared && 
                    (_dirty || 
                    ((_parameters != null) && (_parameters.IsDirty)) || 
                    ((activeConnection != null) && ((activeConnection.CloseCount != _preparedConnectionCloseCount) || (activeConnection.ReconnectCount != _preparedConnectionReconnectCount)))));
            }
            set {
                // only mark the command as dirty if it is already prepared
                // but always clear the value if it we are clearing the dirty flag
                _dirty = value ? IsPrepared : false;
                if (null != _parameters) {
                    _parameters.IsDirty = _dirty;
                }
                _cachedMetaData = null;
            }
        }

        /// <summary>
        /// Get or set the number of records affected by SpDescribeParameterEncryption.
        /// The below line is used only for debug asserts and not exposed publicly or impacts functionality otherwise.
        /// </summary>
        internal int RowsAffectedByDescribeParameterEncryption
        {
            get {
                return _rowsAffectedBySpDescribeParameterEncryption;
            }
            set {
                if (-1 == _rowsAffectedBySpDescribeParameterEncryption) {
                    _rowsAffectedBySpDescribeParameterEncryption = value;
                }
                else if (0 < value) {
                    _rowsAffectedBySpDescribeParameterEncryption += value;
                }
            }
        }

        internal int InternalRecordsAffected {
            get {
                return _rowsAffected;
            }
            set {
                if (-1 == _rowsAffected) {
                    _rowsAffected = value;
                }
                else if (0 < value) {
                    _rowsAffected += value;
                }
            }
        }

        internal bool BatchRPCMode {
            get {
                return _batchRPCMode;
            }
            set {
                _batchRPCMode = value;

                if (_batchRPCMode == false) {
                    ClearBatchCommand();
                } else {
                    if (_RPCList == null) {
                        _RPCList = new List<_SqlRPC>();
                    }
                    if (_parameterCollectionList == null) {
                        _parameterCollectionList = new List<SqlParameterCollection>();
                    }
                }
            }
        }

        /// <summary>
        /// Clear the state in sqlcommand related to describe parameter encryption RPC requests.
        /// </summary>
        private void ClearDescribeParameterEncryptionRequests() {
            _sqlRPCParameterEncryptionReqArray = null;
            _currentlyExecutingDescribeParameterEncryptionRPC = 0;
            _isDescribeParameterEncryptionRPCCurrentlyInProgress = false;
            _rowsAffectedBySpDescribeParameterEncryption = -1;
        }

        internal void ClearBatchCommand() {
            List<_SqlRPC> rpcList = _RPCList;
            if (null != rpcList) {
                rpcList.Clear();
            }
            if (null != _parameterCollectionList) {
                _parameterCollectionList.Clear();
            }
            _SqlRPCBatchArray = null;
            _currentlyExecutingBatch = 0;
        }

        /// <summary>
        /// Set the column encryption setting to the new one.
        /// Do not allow conflicting column encryption settings.
        /// </summary>
        private void SetColumnEncryptionSetting(SqlCommandColumnEncryptionSetting newColumnEncryptionSetting) {
            if (!this._wasBatchModeColumnEncryptionSettingSetOnce) {
                this._columnEncryptionSetting = newColumnEncryptionSetting;
                this._wasBatchModeColumnEncryptionSettingSetOnce = true;
            }
            else {
                if (this._columnEncryptionSetting != newColumnEncryptionSetting) {
                    throw SQL.BatchedUpdateColumnEncryptionSettingMismatch();
                }
            }
        }

            internal void AddBatchCommand(string commandText, SqlParameterCollection parameters, CommandType cmdType, SqlCommandColumnEncryptionSetting columnEncryptionSetting) {
            Debug.Assert(BatchRPCMode, "Command is not in batch RPC Mode");
            Debug.Assert(_RPCList != null);
            Debug.Assert(_parameterCollectionList != null);

            _SqlRPC  rpc = new _SqlRPC();

            this.CommandText = commandText;
            this.CommandType = cmdType;

            // Set the column encryption setting.
            SetColumnEncryptionSetting(columnEncryptionSetting);

            GetStateObject();
            if (cmdType == CommandType.StoredProcedure) {
                BuildRPC(false, parameters, ref rpc);
            }
            else {
                // All batch sql statements must be executed inside sp_executesql, including those without parameters
                BuildExecuteSql(CommandBehavior.Default, commandText, parameters, ref rpc);
            }
             _RPCList.Add(rpc);
             // Always add a parameters collection per RPC, even if there are no parameters.
             _parameterCollectionList.Add(parameters);

            ReliablePutStateObject();
        }

        internal int ExecuteBatchRPCCommand() {

            Debug.Assert(BatchRPCMode, "Command is not in batch RPC Mode");
            Debug.Assert(_RPCList != null, "No batch commands specified");
            _SqlRPCBatchArray = _RPCList.ToArray();
            _currentlyExecutingBatch = 0;
            return ExecuteNonQuery();       // Check permissions, execute, return output params

        }

        internal int? GetRecordsAffected(int commandIndex) {
            Debug.Assert(BatchRPCMode, "Command is not in batch RPC Mode");
            Debug.Assert(_SqlRPCBatchArray != null, "batch command have been cleared");
            return _SqlRPCBatchArray[commandIndex].recordsAffected;
        }

        internal SqlException GetErrors(int commandIndex) {
            SqlException result = null;
            int length = (_SqlRPCBatchArray[commandIndex].errorsIndexEnd - _SqlRPCBatchArray[commandIndex].errorsIndexStart);
            if (0 < length) {
                SqlErrorCollection errors = new SqlErrorCollection();
                for(int i = _SqlRPCBatchArray[commandIndex].errorsIndexStart; i < _SqlRPCBatchArray[commandIndex].errorsIndexEnd; ++i) {
                    errors.Add(_SqlRPCBatchArray[commandIndex].errors[i]);
                }
                for(int i = _SqlRPCBatchArray[commandIndex].warningsIndexStart; i < _SqlRPCBatchArray[commandIndex].warningsIndexEnd; ++i) {
                    errors.Add(_SqlRPCBatchArray[commandIndex].warnings[i]);
                }
                result = SqlException.CreateException(errors, Connection.ServerVersion, Connection.ClientConnectionId);
            }
            return result;
        }
        
        // Allocates and initializes a new SmiRequestExecutor based on the current command state
        private SmiRequestExecutor SetUpSmiRequest( SqlInternalConnectionSmi innerConnection ) {

            // General Approach To Ensure Security of Marshalling:
            //        Only touch each item in the command once
            //        (i.e. only grab a reference to each param once, only
            //        read the type from that param once, etc.).  The problem is
            //        that if the user changes something on the command in the
            //        middle of marshaling, it can overwrite the native buffers
            //        set up.  For example, if max length is used to allocate
            //        buffers, but then re-read from the parameter to truncate
            //        strings, the user could extend the length and overwrite
            //        the buffer.

            if (null != Notification){
                throw SQL.NotificationsNotAvailableOnContextConnection();
            }

            SmiParameterMetaData[] requestMetaData = null;
            ParameterPeekAheadValue[] peekAheadValues = null;

            //    Length of rgMetadata becomes *the* official count of parameters to use,
            //      don't rely on Parameters.Count after this point, as the user could change it.
            int count = GetParameterCount( Parameters );
            if ( 0 < count ) {
                requestMetaData = new SmiParameterMetaData[count];
                peekAheadValues = new ParameterPeekAheadValue[count];

                // set up the metadata
                for ( int index=0; index<count; index++ ) {
                    SqlParameter param = Parameters[index];
                    param.Validate(index, CommandType.StoredProcedure == CommandType);
                    requestMetaData[index] = param.MetaDataForSmi(out peekAheadValues[index]);

                    // Check for valid type for version negotiated
                    if (!innerConnection.IsKatmaiOrNewer) {
                        MetaType mt = MetaType.GetMetaTypeFromSqlDbType(requestMetaData[index].SqlDbType, requestMetaData[index].IsMultiValued);
                        if (!mt.Is90Supported) {
                            throw ADP.VersionDoesNotSupportDataType(mt.TypeName);
                        }
                    }
                }
            }

            // Allocate the new request
            CommandType cmdType = CommandType;
            _smiRequestContext = innerConnection.InternalContext;
            SmiRequestExecutor requestExecutor = _smiRequestContext.CreateRequestExecutor(
                                    CommandText,
                                    cmdType,
                                    requestMetaData,
                                    EventSink
                                );

            // deal with errors
            EventSink.ProcessMessagesAndThrow();

            // Now assign param values
            for ( int index=0; index<count; index++ ) {
                if ( ParameterDirection.Output != requestMetaData[index].Direction &&
                        ParameterDirection.ReturnValue != requestMetaData[index].Direction ) {
                    SqlParameter param = Parameters[index];
                    // going back to command for parameter is ok, since we'll only pick up values now.
                    object value = param.GetCoercedValue();
                    if (value is XmlDataFeed && requestMetaData[index].SqlDbType != SqlDbType.Xml) {
                        value = MetaType.GetStringFromXml(((XmlDataFeed)value)._source);
                    }
                    ExtendedClrTypeCode typeCode = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(requestMetaData[index].SqlDbType, requestMetaData[index].IsMultiValued, value, null /* parameters don't use CLR Type for UDTs */, SmiContextFactory.Instance.NegotiatedSmiVersion);

                    // Handle null reference as special case for parameters
                    if ( CommandType.StoredProcedure == cmdType && 
                                ExtendedClrTypeCode.Empty == typeCode ) {
                        requestExecutor.SetDefault( index );
                    }
                    else {
                        // SQLBU 402391 & 403631: Exception to prevent Parameter.Size data corruption cases from working.
                        //  This should be temporary until changing to correct behavior can be safely implemented.
                        // initial size criteria is the same for all affected types
                        // NOTE: assumes size < -1 is handled by SqlParameter.Size setter
                        int size = param.Size;
                        if (size != 0 && size != SmiMetaData.UnlimitedMaxLengthIndicator && !param.SizeInferred) {
                            switch(requestMetaData[index].SqlDbType) {
                                case SqlDbType.Image:
                                case SqlDbType.Text:
                                    if (size != Int32.MaxValue) {
                                        throw SQL.ParameterSizeRestrictionFailure(index);
                                    }
                                    break;

                                case SqlDbType.NText:
                                    if (size != Int32.MaxValue/2) {
                                        throw SQL.ParameterSizeRestrictionFailure(index);
                                    }
                                    break;

                                case SqlDbType.VarBinary:
                                case SqlDbType.VarChar:
                                    // Allow size==Int32.MaxValue because of DeriveParameters
                                    if (size > 0 && size != Int32.MaxValue && requestMetaData[index].MaxLength == SmiMetaData.UnlimitedMaxLengthIndicator) {
                                        throw SQL.ParameterSizeRestrictionFailure(index);
                                    }
                                    break;

                                case SqlDbType.NVarChar:
                                    // Allow size==Int32.MaxValue/2 because of DeriveParameters
                                    if (size > 0 && size != Int32.MaxValue/2 && requestMetaData[index].MaxLength == SmiMetaData.UnlimitedMaxLengthIndicator) {
                                        throw SQL.ParameterSizeRestrictionFailure(index);
                                    }
                                    break;

                                case SqlDbType.Timestamp:
                                    // Size limiting for larger values will happen due to MaxLength
                                    if (size < SmiMetaData.DefaultTimestamp.MaxLength) {
                                        throw SQL.ParameterSizeRestrictionFailure(index);
                                    }
                                    break;

                                case SqlDbType.Variant:
                                    // Variant problems happen when Size is less than maximums for character and binary values
                                    // Size limiting for larger values will happen due to MaxLength
                                    // NOTE: assumes xml and udt types are handled in parameter value coercion
                                    //      since server does not allow these types in a variant
                                    if (null != value) {
                                        MetaType mt = MetaType.GetMetaTypeFromValue(value);

                                        if ((mt.IsNCharType && size < SmiMetaData.MaxUnicodeCharacters) ||
                                                (mt.IsBinType && size < SmiMetaData.MaxBinaryLength) ||
                                                (mt.IsAnsiType && size < SmiMetaData.MaxANSICharacters)) {
                                            throw SQL.ParameterSizeRestrictionFailure(index);
                                        }
                                    }
                                    break;

                                 case SqlDbType.Xml:
                                    // Xml is an issue for non-SqlXml types
                                    if (null != value && ExtendedClrTypeCode.SqlXml != typeCode) {
                                        throw SQL.ParameterSizeRestrictionFailure(index);
                                    }
                                    break;

                                 // NOTE: Char, NChar, Binary and UDT do not need restricting because they are always 8k or less, 
                                 //         so the metadata MaxLength will match the Size setting.

                                default:
                                    break;
                            }
                        }

                        if (innerConnection.IsKatmaiOrNewer) {
                            ValueUtilsSmi.SetCompatibleValueV200(EventSink, requestExecutor, index, requestMetaData[index], value, typeCode, param.Offset, param.Size, peekAheadValues[index]);
                        }
                        else {
                            ValueUtilsSmi.SetCompatibleValue( EventSink, requestExecutor, index, requestMetaData[index], value, typeCode, param.Offset );
                        }
                    }
                }
            }

            return requestExecutor;
        }

        private void WriteBeginExecuteEvent()
        {
            if (SqlEventSource.Log.IsEnabled() && Connection != null)
            {
                string commandText = CommandType == CommandType.StoredProcedure ? CommandText : string.Empty;
                SqlEventSource.Log.BeginExecute(GetHashCode(), Connection.DataSource, Connection.Database, commandText);
            }
        }

        /// <summary>
        /// Writes and end execute event in Event Source.
        /// </summary>
        /// <param name="success">True if SQL command finished successfully, otherwise false.</param>
        /// <param name="sqlExceptionNumber">Gets a number that identifies the type of error.</param>
        /// <param name="synchronous">True if SQL command was executed synchronously, otherwise false.</param>
        private void WriteEndExecuteEvent(bool success, int? sqlExceptionNumber, bool synchronous)
        {
            if (SqlEventSource.Log.IsEnabled())
            {
                // SqlEventSource.WriteEvent(int, int, int, int) is faster than provided overload SqlEventSource.WriteEvent(int, object[]).
                // that's why trying to fit several booleans in one integer value

                // success state is stored the first bit in compositeState 0x01
                int successFlag = success ? 1 : 0;

                // isSqlException is stored in the 2nd bit in compositeState 0x100
                int isSqlExceptionFlag = sqlExceptionNumber.HasValue ? 2 : 0;

                // synchronous state is stored in the second bit in compositeState 0x10
                int synchronousFlag = synchronous ? 4 : 0;

                int compositeState = successFlag | isSqlExceptionFlag | synchronousFlag;

                SqlEventSource.Log.EndExecute(GetHashCode(), compositeState, sqlExceptionNumber.GetValueOrDefault());
            }
        }

#if DEBUG
        internal void CompletePendingReadWithSuccess(bool resetForcePendingReadsToWait) {
            var stateObj = _stateObj;
            if (stateObj != null) {
                stateObj.CompletePendingReadWithSuccess(resetForcePendingReadsToWait);
            }
            else {
                var tempCachedAsyncState = cachedAsyncState;
                if (tempCachedAsyncState != null) {
                    var reader = tempCachedAsyncState.CachedAsyncReader;
                    if (reader != null) {
                        reader.CompletePendingReadWithSuccess(resetForcePendingReadsToWait);
                    }
                }
            }
        }

        internal void CompletePendingReadWithFailure(int errorCode, bool resetForcePendingReadsToWait) {
            var stateObj = _stateObj;
            if (stateObj != null) {
                stateObj.CompletePendingReadWithFailure(errorCode, resetForcePendingReadsToWait);
            }
            else {
                var tempCachedAsyncState = _cachedAsyncState;
                if (tempCachedAsyncState != null) {
                    var reader = tempCachedAsyncState.CachedAsyncReader;
                    if (reader != null) {
                        reader.CompletePendingReadWithFailure(errorCode, resetForcePendingReadsToWait);
                    }
                }
            }
        }
#endif
    }
}