-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathSqlDataAdapter.xml
More file actions
1279 lines (1156 loc) · 66.1 KB
/
SqlDataAdapter.xml
File metadata and controls
1279 lines (1156 loc) · 66.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<docs>
<members name="SqlDataAdapter">
<SqlDataAdapter>
<summary>
Represents a set of data commands and a database connection that are used to fill the <see cref="T:System.Data.DataSet" /> and update a SQL Server database. This class cannot be inherited.
</summary>
<remarks>
<format type="text/markdown"><.
<xref:Microsoft.Data.SqlClient.SqlDataAdapter> is used in conjunction with <xref:Microsoft.Data.SqlClient.SqlConnection> and <xref:Microsoft.Data.SqlClient.SqlCommand> to increase performance when connecting to a SQL Server database.
> [!NOTE]
> If you are using SQL Server stored procedures to edit or delete data using a `DataAdapter`, make sure that you do not use SET NOCOUNT ON in the stored procedure definition. This causes the rows affected count returned to be zero, which the `DataAdapter` interprets as a concurrency conflict. In this event, a <xref:System.Data.DBConcurrencyException> will be thrown.
The <xref:Microsoft.Data.SqlClient.SqlDataAdapter> also includes the <xref:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand%2A>, <xref:Microsoft.Data.SqlClient.SqlDataAdapter.InsertCommand%2A>, <xref:Microsoft.Data.SqlClient.SqlDataAdapter.DeleteCommand%2A>, <xref:Microsoft.Data.SqlClient.SqlDataAdapter.UpdateCommand%2A>, and <xref:System.Data.Common.DataAdapter.TableMappings%2A> properties to facilitate the loading and updating of data.
When an instance of <xref:Microsoft.Data.SqlClient.SqlDataAdapter> is created, the read/write properties are set to initial values. For a list of these values, see the <xref:Microsoft.Data.SqlClient.SqlDataAdapter> constructor.
The <xref:Microsoft.Data.SqlClient.SqlDataAdapter.InsertCommand%2A>, <xref:Microsoft.Data.SqlClient.SqlDataAdapter.DeleteCommand%2A>, and <xref:Microsoft.Data.SqlClient.SqlDataAdapter.UpdateCommand%2A> are generic templates that are automatically filled with individual values from every modified row through the parameters mechanism.
For every column that you propagate to the data source on <xref:System.Data.Common.DbDataAdapter.Update%2A>, a parameter should be added to the `InsertCommand`, `UpdateCommand`, or `DeleteCommand`. The <xref:System.Data.Common.DbParameter.SourceColumn%2A> property of the <xref:System.Data.Common.DbParameter> object should be set to the name of the column. This setting indicates that the value of the parameter is not set manually, but is taken from the particular column in the currently processed row.
> [!NOTE]
> An <xref:System.InvalidOperationException> will occur if the <xref:System.Data.Common.DbDataAdapter.Fill%2A> method is called and the table contains a user-defined type that is not available on the client computer. For more information, see [CLR User-Defined Types](https://learn.microsoft.com/sql/relational-databases/clr-integration-database-objects-user-defined-types/clr-user-defined-types).
## Examples
The following example uses the <xref:Microsoft.Data.SqlClient.SqlCommand>, <xref:Microsoft.Data.SqlClient.SqlDataAdapter>, and <xref:Microsoft.Data.SqlClient.SqlConnection> to select records from a database and populate a <xref:System.Data.DataSet> with the selected rows. The filled <xref:System.Data.DataSet> is then returned. To accomplish this, the method is passed an initialized <xref:System.Data.DataSet>, a connection string, and a query string that is a Transact-SQL SELECT statement.
[!code-csharp[SqlDataAdapter_SelectCommand Example#1](~/../sqlclient/doc/samples/SqlDataAdapter_SelectCommand.cs#1)]
]]></format>
</remarks>
</SqlDataAdapter>
<ctor2>
<summary>
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> class.
</summary>
<remarks>
<para>
When an instance of <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> is created, the following read/write properties are set to the following initial values.
</para>
<list type="table">
<listheader>
<term>
Properties
</term>
<description>
Initial value
</description>
</listheader>
<item>
<term><see cref="P:System.Data.Common.DataAdapter.MissingMappingAction" /></term>
<description><see cref="F:System.Data.MissingMappingAction.Passthrough" /></description>
</item>
<item>
<term><see cref="P:System.Data.Common.DataAdapter.MissingSchemaAction" /></term>
<description><see cref="F:System.Data.MissingSchemaAction.Add" /></description>
</item>
</list>
<para>
You can change the value of these properties through a separate call to the property.
</para>
</remarks>
<example>
<para>
The following example creates a <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> and sets some of its properties.
</para>
<!-- SqlDataAdapter_SqlDataAdapter -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
}
public static SqlDataAdapter CreateSqlDataAdapter(SqlConnection connection)
{
// Assumes that connection is a valid SqlConnection object
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
// Create the commands.
adapter.SelectCommand = new SqlCommand(
"SELECT CustomerID, CompanyName FROM CUSTOMERS", connection);
adapter.InsertCommand = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);
adapter.UpdateCommand = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);
adapter.DeleteCommand = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
// Create the parameters.
adapter.InsertCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID");
adapter.InsertCommand.Parameters.Add(
"@CompanyName",
SqlDbType.VarChar,
40,
"CompanyName");
adapter.UpdateCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID");
adapter.UpdateCommand.Parameters.Add(
"@CompanyName",
SqlDbType.VarChar,
40,
"CompanyName");
adapter.UpdateCommand.Parameters.Add(
"@oldCustomerID",
SqlDbType.Char,
5,
"CustomerID"
).SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID"
).SourceVersion = DataRowVersion.Original;
return adapter;
}
public static SqlDataAdapter CustomerUpdateCommand(SqlDataAdapter adapter)
{
// Assumes that connection is a valid SqlAdapter object
adapter.UpdateCommand.Parameters.Add(
"@CompanyName",
SqlDbType.VarChar,
15,
"CompanyName");
SqlParameter parameter = adapter.UpdateCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
return adapter;
}
}
</code>
</example>
</ctor2>
<ctorSelectCommand>
<param name="selectCommand">
A <see cref="T:Microsoft.Data.SqlClient.SqlCommand" /> that is a Transact-SQL SELECT statement or stored procedure and is set as the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> property of the <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" />.
</param>
<summary>
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> class with the specified <see cref="T:Microsoft.Data.SqlClient.SqlCommand" /> as the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> property.
</summary>
<remarks>
<para>
This implementation of the <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> constructor sets the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> property to the value specified in the <paramref name="selectCommand" /> parameter. When an instance of <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> is created, the following read/write properties are set to the following initial values.
</para>
<list type="table">
<listheader>
<term>Properties</term>
<description>Initial value</description>
</listheader>
<item>
<term><see cref="P:System.Data.Common.DataAdapter.MissingMappingAction" /></term>
<description><see cref="F:System.Data.MissingMappingAction.Passthrough" /></description>
</item>
<item>
<term><see cref="P:System.Data.Common.DataAdapter.MissingSchemaAction" /></term>
<description><see cref="F:System.Data.MissingSchemaAction.Add" /></description>
</item>
</list>
<para>
You can change the value of these properties through a separate call to the property.
</para>
<para>
When <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> (or any of the other command properties) is assigned to a previously created <see cref="T:Microsoft.Data.SqlClient.SqlCommand" />, the <see cref="T:Microsoft.Data.SqlClient.SqlCommand" /> is not cloned. The <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> maintains a reference to the previously created <see cref="T:Microsoft.Data.SqlClient.SqlCommand" /> object.
</para>
</remarks>
<example>
<para>
The following example creates a <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> and sets some of its properties.
</para>
<!-- SqlDataAdapter_SqlDataAdapter1 -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
}
public static SqlDataAdapter CreateSqlDataAdapter(SqlCommand selectCommand, SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
// Create the other commands.
adapter.InsertCommand = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);
adapter.UpdateCommand = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);
adapter.DeleteCommand = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
// Create the parameters.
adapter.InsertCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID");
adapter.InsertCommand.Parameters.Add(
"@CompanyName",
SqlDbType.VarChar,
40,
"CompanyName");
adapter.UpdateCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID");
adapter.UpdateCommand.Parameters.Add(
"@CompanyName",
SqlDbType.VarChar,
40,
"CompanyName");
adapter.UpdateCommand.Parameters.Add(
"@oldCustomerID",
SqlDbType.Char,
5,
"CustomerID"
).SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID"
).SourceVersion = DataRowVersion.Original;
return adapter;
}
}
</code>
</example>
</ctorSelectCommand>
<ctorSelectCommandTextSelectConnection>
<param name="selectCommandText">
A <see cref="T:System.String" /> that is a Transact-SQL SELECT statement or stored procedure to be used by the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> property of the <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" />.
</param>
<param name="selectConnection">
A <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> that represents the connection. If your connection string does not use <see langword="Integrated Security = true" />, you can use <see cref="T:Microsoft.Data.SqlClient.SqlCredential" /> to pass the user ID and password more securely than by specifying the user ID and password as text in the connection string.
</param>
<summary>
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> class with a <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> and a <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> object.
</summary>
<remarks>
<para>
This implementation of the <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> opens and closes a <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> if it is not already open. This can be useful in an application that must call the <see cref="M:System.Data.Common.DbDataAdapter.Fill(System.Data.DataSet)" /> method for two or more <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> objects. If the <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> is already open, you must explicitly call <see cref="M:Microsoft.Data.SqlClient.SqlConnection.Close" /> or <see cref="M:System.IDisposable.Dispose" /> to close it.
</para>
<para>
When an instance of <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> is created, the following read/write properties are set to the following initial values.
</para>
<list type="table">
<listheader>
<term>
Properties
</term>
<description>
Initial value
</description>
</listheader>
<item>
<term><see cref="P:System.Data.Common.DataAdapter.MissingMappingAction" /></term>
<description><see cref="F:System.Data.MissingMappingAction.Passthrough" /></description>
</item>
<item>
<term><see cref="P:System.Data.Common.DataAdapter.MissingSchemaAction" /></term>
<description><see cref="F:System.Data.MissingSchemaAction.Add" /></description>
</item>
</list>
<para>
You can change the value of either of these properties through a separate call to the property.
</para>
</remarks>
<example>
<para>
The following example creates a <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> and sets some of its properties.
</para>
<!-- SqlDataAdapter_SqlDataAdapter3 -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
}
public static SqlDataAdapter CreateSqlDataAdapter(string commandText, SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter(commandText, connection);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
// Create the other commands.
adapter.InsertCommand = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)");
adapter.UpdateCommand = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID");
adapter.DeleteCommand = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID");
// Create the parameters.
adapter.InsertCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID");
adapter.InsertCommand.Parameters.Add(
"@CompanyName",
SqlDbType.VarChar,
40,
"CompanyName");
adapter.UpdateCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID");
adapter.UpdateCommand.Parameters.Add(
"@CompanyName",
SqlDbType.VarChar,
40,
"CompanyName");
adapter.UpdateCommand.Parameters.Add(
"@oldCustomerID",
SqlDbType.Char,
5,
"CustomerID"
).SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID"
).SourceVersion = DataRowVersion.Original;
return adapter;
}
}
</code>
</example>
</ctorSelectCommandTextSelectConnection>
<ctorSelectCommandTextSelectConnectionString>
<param name="selectCommandText">
A <see cref="T:System.String" /> that is a Transact-SQL SELECT statement or stored procedure to be used by the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> property of the <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" />.
</param>
<param name="selectConnectionString">
The connection string. If your connection string does not use <see langword="Integrated Security = true" />, you can use <see cref="M:Microsoft.Data.SqlClient.SqlDataAdapter.#ctor(System.String,Microsoft.Data.SqlClient.SqlConnection)" /> and <see cref="T:Microsoft.Data.SqlClient.SqlCredential" /> to pass the user ID and password more securely than by specifying the user ID and password as text in the connection string.
</param>
<summary>
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> class with a <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> and a connection string.
</summary>
<remarks>
<para>
This overload of the <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> constructor uses the <paramref name="selectCommandText" /> parameter to set the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> property. The <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> will create and maintain the connection created with the <paramref name="selectConnectionString" /> parameter.
</para>
<para>
When an instance of <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> is created, the following read/write properties are set to the following initial values.
</para>
<list type="table">
<listheader>
<term>
Properties
</term>
<description>
Initial value
</description>
</listheader>
<item>
<term><see cref="P:System.Data.Common.DataAdapter.MissingMappingAction" /></term>
<description><see cref="F:System.Data.MissingMappingAction.Passthrough" /></description>
</item>
<item>
<term><see cref="P:System.Data.Common.DataAdapter.MissingSchemaAction" /></term>
<description><see cref="F:System.Data.MissingSchemaAction.Add" /></description>
</item>
</list>
<para>
You can change the value of these properties through a separate call to the property.
</para>
</remarks>
<example>
<para>
The following example creates a <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> and sets some of its properties.
</para>
<!-- SqlDataAdapter_SqlDataAdapter2 -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
}
public static SqlDataAdapter CreateSqlDataAdapter(string commandText, string connectionString)
{
SqlDataAdapter adapter = new SqlDataAdapter(commandText, connectionString);
SqlConnection connection = adapter.SelectCommand.Connection;
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
// Create the commands.
adapter.InsertCommand = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);
adapter.UpdateCommand = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);
adapter.DeleteCommand = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
// Create the parameters.
adapter.InsertCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID");
adapter.InsertCommand.Parameters.Add(
"@CompanyName",
SqlDbType.VarChar,
40,
"CompanyName");
adapter.UpdateCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID");
adapter.UpdateCommand.Parameters.Add(
"@CompanyName",
SqlDbType.VarChar,
40,
"CompanyName");
adapter.UpdateCommand.Parameters.Add(
"@oldCustomerID",
SqlDbType.Char,
5,
"CustomerID"
).SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand.Parameters.Add(
"@CustomerID",
SqlDbType.Char,
5,
"CustomerID"
).SourceVersion = DataRowVersion.Original;
return adapter;
}
}
</code>
</example>
</ctorSelectCommandTextSelectConnectionString>
<AddToBatch>
<param name="command">
To be added.
</param>
<summary>
To be added.
</summary>
<returns>
To be added.
</returns>
<remarks>
To be added.
</remarks>
</AddToBatch>
<ClearBatch>
<summary>
To be added.
</summary>
<remarks>
To be added.
</remarks>
</ClearBatch>
<CreateRowUpdatedEvent>
<param name="dataRow">
To be added.
</param>
<param name="command">
To be added.
</param>
<param name="statementType">
To be added.
</param>
<param name="tableMapping">
To be added.
</param>
<summary>
To be added.
</summary>
<returns>
To be added.
</returns>
<remarks>
To be added.
</remarks>
</CreateRowUpdatedEvent>
<CreateRowUpdatingEvent>
<param name="dataRow">
To be added.
</param>
<param name="command">
To be added.
</param>
<param name="statementType">
To be added.
</param>
<param name="tableMapping">
To be added.
</param>
<summary>
To be added.
</summary>
<returns>
To be added.
</returns>
<remarks>
To be added.
</remarks>
</CreateRowUpdatingEvent>
<DeleteCommand>
<summary>
Gets or sets a Transact-SQL statement or stored procedure to delete records from the data set.
</summary>
<value>
A <see cref="T:Microsoft.Data.SqlClient.SqlCommand" /> used during <see cref="M:System.Data.Common.DbDataAdapter.Update(System.Data.DataSet)" /> to delete records in the database that correspond to deleted rows in the <see cref="T:System.Data.DataSet" />.
</value>
<remarks>
<para>
During <see cref="M:System.Data.Common.DbDataAdapter.Update(System.Data.DataSet)" />, if this property is not set and primary key information is present in the <see cref="T:System.Data.DataSet" />, the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.DeleteCommand" /> can be generated automatically if you set the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> property and use the <see cref="T:Microsoft.Data.SqlClient.SqlCommandBuilder" />. Then, any additional commands that you do not set are generated by the <see cref="T:Microsoft.Data.SqlClient.SqlCommandBuilder" />. This generation logic requires key column information to be present in the <see cref="T:System.Data.DataSet" />. For more information, see <see href="https://learn.microsoft.com/sql/connect/ado-net/generate-commands-with-commandbuilders">Generating Commands with CommandBuilders</see>.
</para>
<para>
When <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.DeleteCommand" /> is assigned to a previously created <see cref="T:Microsoft.Data.SqlClient.SqlCommand" />, the <see cref="T:Microsoft.Data.SqlClient.SqlCommand" /> is not cloned. The <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.DeleteCommand" /> maintains a reference to the previously created <see cref="T:Microsoft.Data.SqlClient.SqlCommand" /> object.
</para>
<para>
For every column that you propagate to the data source on <see cref="M:System.Data.Common.DbDataAdapter.Update(System.Data.DataSet)" />, a parameter should be added to the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.InsertCommand" />, <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.UpdateCommand" />, or <b>DeleteCommand</b>. The <c>SourceColumn</c> property of the parameter should be set to the name of the column. This indicates that the value of the parameter is not set manually, but is taken from the particular column in the currently processed row.
</para>
</remarks>
<example>
<para>
The following example creates a <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> and sets the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" />, <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.InsertCommand" />, <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.UpdateCommand" />, and <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.DeleteCommand" /> properties. It assumes you have already created a <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> object.
</para>
<!-- SqlDataAdapter -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
}
public static SqlDataAdapter CreateCustomerAdapter(SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter();
// Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection);
// Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15);
adapter.SelectCommand = command;
// Create the InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);
// Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
adapter.InsertCommand = command;
// Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);
// Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.UpdateCommand = command;
// Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand = command;
return adapter;
}
}
</code>
</example>
</DeleteCommand>
<ExecuteBatch>
<summary>
To be added.
</summary>
<returns>
To be added.
</returns>
<remarks>
To be added.
</remarks>
</ExecuteBatch>
<GetBatchedParameter>
<param name="commandIdentifier">
To be added.
</param>
<param name="parameterIndex">
To be added.
</param>
<summary>
To be added.
</summary>
<returns>
To be added.
</returns>
<remarks>
To be added.
</remarks>
</GetBatchedParameter>
<GetBatchedRecordsAffected>
<param name="commandIdentifier">
To be added.
</param>
<param name="recordsAffected">
To be added.
</param>
<param name="error">
To be added.
</param>
<summary>
To be added.
</summary>
<returns>
To be added.
</returns>
<remarks>
To be added.
</remarks>
</GetBatchedRecordsAffected>
<InitializeBatching>
<summary>
To be added.
</summary>
<remarks>
To be added.
</remarks>
</InitializeBatching>
<InsertCommand>
<summary>
Gets or sets a Transact-SQL statement or stored procedure to insert new records into the data source.
</summary>
<value>
A <see cref="T:Microsoft.Data.SqlClient.SqlCommand" /> used during <see cref="M:System.Data.Common.DbDataAdapter.Update(System.Data.DataSet)" /> to insert records into the database that correspond to new rows in the <see cref="T:System.Data.DataSet" />.
</value>
<remarks>
<para>
During <see cref="M:System.Data.Common.DbDataAdapter.Update(System.Data.DataSet)" />, if this property is not set and primary key information is present in the <see cref="T:System.Data.DataSet" />, the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.InsertCommand" /> can be generated automatically if you set the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" /> property and use the <see cref="T:Microsoft.Data.SqlClient.SqlCommandBuilder" />. Then, any additional commands that you do not set are generated by the <see cref="T:Microsoft.Data.SqlClient.SqlCommandBuilder" />. This generation logic requires key column information to be present in the <see cref="T:System.Data.DataSet" />. For more information, see <see href="https://learn.microsoft.com/sql/connect/ado-net/generate-commands-with-commandbuilders">Generating Commands with CommandBuilders</see>.
</para>
<para>
When <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.InsertCommand" /> is assigned to a previously created <see cref="T:Microsoft.Data.SqlClient.SqlCommand" />, the <see cref="T:Microsoft.Data.SqlClient.SqlCommand" /> is not cloned. The <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.InsertCommand" /> maintains a reference to the previously created <see cref="T:Microsoft.Data.SqlClient.SqlCommand" /> object.
</para>
<para>
If execution of this command returns rows, these rows can be added to the <see cref="T:System.Data.DataSet" /> depending on how you set the <b>UpdatedRowSource</b> property of the <see cref="T:Microsoft.Data.SqlClient.SqlCommand" /> object.
</para>
<para>
For every column that you propagate to the data source on <see cref="M:System.Data.Common.DbDataAdapter.Update(System.Data.DataSet)" />, a parameter should be added to <b>InsertCommand</b>, <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.UpdateCommand" />, or <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.DeleteCommand" />. The <c>SourceColumn</c> property of the parameter should be set to the name of the column. This indicates that the value of the parameter is not set manually, but is taken from the particular column in the currently processed row.
</para>
</remarks>
<example>
<para>
The following example creates a <see cref="T:Microsoft.Data.SqlClient.SqlDataAdapter" /> and sets the <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.SelectCommand" />, <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.InsertCommand" />, <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.UpdateCommand" />, and <see cref="P:Microsoft.Data.SqlClient.SqlDataAdapter.DeleteCommand" /> properties. It assumes you have already created a <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> object.
</para>
<!-- SqlDataAdapter -->
<code language="c#">
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
}
public static SqlDataAdapter CreateCustomerAdapter(SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter();
// Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection);
// Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15);
adapter.SelectCommand = command;
// Create the InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);
// Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
adapter.InsertCommand = command;
// Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);
// Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.UpdateCommand = command;
// Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand = command;
return adapter;
}
}
</code>
</example>
</InsertCommand>
<OnRowUpdated>
<param name="value">
To be added.
</param>
<summary>
To be added.
</summary>
<remarks>
To be added.
</remarks>
</OnRowUpdated>
<OnRowUpdating>
<param name="value">
To be added.
</param>
<summary>
To be added.
</summary>
<remarks>
To be added.
</remarks>
</OnRowUpdating>
<RowUpdated>
<summary>
Occurs during <see cref="M:System.Data.Common.DbDataAdapter.Update(System.Data.DataSet)" /> after a command is executed against the data source for each updated row. Use this event to inspect the result, check for errors, or control how <see cref="M:System.Data.DataRow.AcceptChanges" /> is applied.
</summary>
<remarks>
<para>
When using <see cref="M:System.Data.Common.DbDataAdapter.Update(System.Data.DataSet)" />, there are two events that occur per data row updated. The order of execution is as follows:
</para>
<list type="number">
<item><description>The values in the <see cref="T:System.Data.DataRow" /> are moved to the parameter values.</description></item>
<item><description>The <see cref="E:Microsoft.Data.SqlClient.SqlDataAdapter.RowUpdating" /> event is raised.</description></item>
<item><description>The command executes.</description></item>
<item><description>If the command's <see cref="P:System.Data.IDbCommand.UpdatedRowSource" /> property is set to <see cref="F:System.Data.UpdateRowSource.FirstReturnedRecord" />, the first returned result is placed in the <see cref="T:System.Data.DataRow" />.</description></item>
<item><description>If there are output parameters, they are placed in the <see cref="T:System.Data.DataRow" />.</description></item>
<item><description>The <see cref="E:Microsoft.Data.SqlClient.SqlDataAdapter.RowUpdated" /> event is raised.</description></item>
<item><description><see cref="M:System.Data.DataRow.AcceptChanges" /> is called.</description></item>
</list>
</remarks>
<example>
<para>
The following example shows how to use both the <see cref="E:Microsoft.Data.SqlClient.SqlDataAdapter.RowUpdating" /> and <see cref="E:Microsoft.Data.SqlClient.SqlDataAdapter.RowUpdated" /> events.
</para>
<!-- SqlDataAdapter_RowUpdated -->
<code language="c#">
using System;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using System.Xml;
using Microsoft.Data.SqlClient;
public class Form1 : Form
{
private DataSet DataSet1;
private DataGrid dataGrid1;
// handler for RowUpdating event
private static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
PrintEventArgs(e);
}
// handler for RowUpdated event
private static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
PrintEventArgs(e);
}
public static int Main()
{
const string connectionString = "Integrated Security=SSPI;database=Northwind;server=MSSQL1";
const string queryString = "SELECT * FROM Products";
// create DataAdapter
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connectionString);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
// Create and fill DataSet (select only first 5 rows)
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, 0, 5, "Table");
// Modify DataSet
DataTable table = dataSet.Tables["Table"];
table.Rows[0][1] = "new product";
// add handlers
adapter.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating);
adapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
// update, this operation fires two events
// (RowUpdating/RowUpdated) per changed row
adapter.Update(dataSet, "Table");
// remove handlers
adapter.RowUpdating -= new SqlRowUpdatingEventHandler(OnRowUpdating);
adapter.RowUpdated -= new SqlRowUpdatedEventHandler(OnRowUpdated);
return 0;
}
private static void PrintEventArgs(SqlRowUpdatingEventArgs args)
{
Console.WriteLine("OnRowUpdating");
Console.WriteLine(" event args: (" +
" command=" + args.Command +
" commandType=" + args.StatementType +
" status=" + args.Status + ")");
}
private static void PrintEventArgs(SqlRowUpdatedEventArgs args)
{
Console.WriteLine("OnRowUpdated");
Console.WriteLine(" event args: (" +
" command=" + args.Command +
" commandType=" + args.StatementType +
" recordsAffected=" + args.RecordsAffected +
" status=" + args.Status + ")");
}
}
</code>
<para>
The <see cref="E:Microsoft.Data.SqlClient.SqlDataAdapter.RowUpdating" /> event returns this output:
</para>
<code>
event args: (command=Microsoft.Data.SqlClient.SqlCommand commandType=2 status=0)
</code>
<para>
The <see cref="E:Microsoft.Data.SqlClient.SqlDataAdapter.RowUpdated" /> event returns this output:
</para>
<code>
event args: (command=Microsoft.Data.SqlClient.SqlCommand commandType=2 recordsAffected=1 row=System.Data.DataRow[37] status=0)
</code>
</example>
</RowUpdated>
<RowUpdating>
<summary>
Occurs during <see cref="M:System.Data.Common.DbDataAdapter.Update(System.Data.DataSet)" /> before a command is executed against the data source for each updated row. Use this event to inspect or modify the command, skip the row, or cancel the update.
</summary>
<remarks>
<para>
When using <see cref="M:System.Data.Common.DbDataAdapter.Update(System.Data.DataSet)" />, there are two events that occur per data row updated. The order of execution is as follows:
</para>
<list type="number">
<item><description>The values in the <see cref="T:System.Data.DataRow" /> are moved to the parameter values.</description></item>
<item><description>The <see cref="E:Microsoft.Data.SqlClient.SqlDataAdapter.RowUpdating" /> event is raised.</description></item>
<item><description>The command executes.</description></item>
<item><description>If the command's <see cref="P:System.Data.IDbCommand.UpdatedRowSource" /> property is set to <see cref="F:System.Data.UpdateRowSource.FirstReturnedRecord" />, the first returned result is placed in the <see cref="T:System.Data.DataRow" />.</description></item>
<item><description>If there are output parameters, they are placed in the <see cref="T:System.Data.DataRow" />.</description></item>
<item><description>The <see cref="E:Microsoft.Data.SqlClient.SqlDataAdapter.RowUpdated" /> event is raised.</description></item>
<item><description><see cref="M:System.Data.DataRow.AcceptChanges" /> is called.</description></item>
</list>
</remarks>
<example>
<para>
The following example shows how to use both the <see cref="E:Microsoft.Data.SqlClient.SqlDataAdapter.RowUpdating" /> and <see cref="E:Microsoft.Data.SqlClient.SqlDataAdapter.RowUpdated" /> events.
</para>
<!-- SqlDataAdapter_RowUpdated -->
<code language="c#">
using System;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using System.Xml;
using Microsoft.Data.SqlClient;
public class Form1 : Form
{
private DataSet DataSet1;
private DataGrid dataGrid1;
// handler for RowUpdating event
private static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
PrintEventArgs(e);
}
// handler for RowUpdated event
private static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
PrintEventArgs(e);
}
public static int Main()
{
const string connectionString = "Integrated Security=SSPI;database=Northwind;server=MSSQL1";
const string queryString = "SELECT * FROM Products";
// create DataAdapter
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connectionString);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
// Create and fill DataSet (select only first 5 rows)
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, 0, 5, "Table");
// Modify DataSet