DbDataAdapter.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. //
  2. // System.Data.Common.DbDataAdapter.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // (C) Ximian, Inc
  9. // Copyright (C) Tim Coleman, 2002-2003
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Runtime.InteropServices;
  16. namespace System.Data.Common {
  17. public abstract class DbDataAdapter : DataAdapter, ICloneable
  18. {
  19. #region Fields
  20. public const string DefaultSourceTableName = "Table";
  21. const string DefaultSourceColumnName = "Column";
  22. #endregion // Fields
  23. #region Constructors
  24. protected DbDataAdapter()
  25. {
  26. }
  27. #endregion // Fields
  28. #region Properties
  29. #if NET_1_2
  30. [MonoTODO]
  31. protected virtual IDbConnection BaseConnection {
  32. get { throw new NotImplementedException (); }
  33. set { throw new NotImplementedException (); }
  34. }
  35. public IDbConnection Connection {
  36. get { return BaseConnection; }
  37. set { BaseConnection = value; }
  38. }
  39. #endif
  40. IDbCommand DeleteCommand {
  41. get { return ((IDbDataAdapter) this).DeleteCommand; }
  42. }
  43. #if NET_1_2
  44. protected internal CommandBehavior FillCommandBehavior {
  45. get { throw new NotImplementedException (); }
  46. set { throw new NotImplementedException (); }
  47. }
  48. #endif
  49. IDbCommand InsertCommand {
  50. get { return ((IDbDataAdapter) this).InsertCommand; }
  51. }
  52. #if NET_1_2
  53. [MonoTODO]
  54. protected virtual IDbCommand this [[Optional] StatementType statementType] {
  55. get { throw new NotImplementedException (); }
  56. set { throw new NotImplementedException (); }
  57. }
  58. protected virtual DbProviderFactory ProviderFactory {
  59. get { throw new NotImplementedException (); }
  60. }
  61. #endif
  62. IDbCommand SelectCommand {
  63. get { return ((IDbDataAdapter) this).SelectCommand; }
  64. }
  65. #if NET_1_2
  66. [MonoTODO]
  67. public IDbTransaction Transaction {
  68. get { throw new NotImplementedException (); }
  69. set { throw new NotImplementedException (); }
  70. }
  71. [MonoTODO]
  72. public int UpdateBatchSize {
  73. get { throw new NotImplementedException (); }
  74. set { throw new NotImplementedException (); }
  75. }
  76. #endif
  77. IDbCommand UpdateCommand {
  78. get { return ((IDbDataAdapter) this).UpdateCommand; }
  79. }
  80. #endregion // Properties
  81. #region Events
  82. #if ONLY_1_0 || ONLY_1_1
  83. [DataCategory ("Fill")]
  84. [DataSysDescription ("Event triggered when a recoverable error occurs during Fill.")]
  85. public event FillErrorEventHandler FillError;
  86. #endif
  87. #endregion // Events
  88. #region Methods
  89. #if NET_1_2
  90. [MonoTODO]
  91. public virtual void BeginInit ()
  92. {
  93. throw new NotImplementedException ();
  94. }
  95. #endif
  96. protected abstract RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping);
  97. protected abstract RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping);
  98. private FillErrorEventArgs CreateFillErrorEvent (DataTable dataTable, object[] values, Exception e)
  99. {
  100. FillErrorEventArgs args = new FillErrorEventArgs (dataTable, values);
  101. args.Errors = e;
  102. args.Continue = false;
  103. return args;
  104. }
  105. protected override void Dispose (bool disposing)
  106. {
  107. if (disposing) {
  108. IDbDataAdapter da = (IDbDataAdapter) this;
  109. if (da.SelectCommand != null) {
  110. da.SelectCommand.Dispose();
  111. da.SelectCommand = null;
  112. }
  113. if (da.InsertCommand != null) {
  114. da.InsertCommand.Dispose();
  115. da.InsertCommand = null;
  116. }
  117. if (da.UpdateCommand != null) {
  118. da.UpdateCommand.Dispose();
  119. da.UpdateCommand = null;
  120. }
  121. if (da.DeleteCommand != null) {
  122. da.DeleteCommand.Dispose();
  123. da.DeleteCommand = null;
  124. }
  125. }
  126. }
  127. #if NET_1_2
  128. [MonoTODO]
  129. public virtual void EndInit ()
  130. {
  131. throw new NotImplementedException ();
  132. }
  133. #endif
  134. public override int Fill (DataSet dataSet)
  135. {
  136. return Fill (dataSet, 0, 0, DefaultSourceTableName, SelectCommand, CommandBehavior.Default);
  137. }
  138. public int Fill (DataTable dataTable)
  139. {
  140. if (dataTable == null)
  141. throw new NullReferenceException ();
  142. return Fill (dataTable, SelectCommand, CommandBehavior.Default);
  143. }
  144. public int Fill (DataSet dataSet, string srcTable)
  145. {
  146. return Fill (dataSet, 0, 0, srcTable, SelectCommand, CommandBehavior.Default);
  147. }
  148. #if NET_1_2
  149. protected override int Fill (DataTable dataTable, IDataReader dataReader)
  150. #else
  151. protected virtual int Fill (DataTable dataTable, IDataReader dataReader)
  152. #endif
  153. {
  154. if (dataReader.FieldCount == 0) {
  155. dataReader.Close ();
  156. return 0;
  157. }
  158. int count = 0;
  159. try {
  160. string tableName = SetupSchema (SchemaType.Mapped, dataTable.TableName);
  161. if (tableName != null) {
  162. dataTable.TableName = tableName;
  163. FillTable (dataTable, dataReader, 0, 0, ref count);
  164. }
  165. } finally {
  166. dataReader.Close ();
  167. }
  168. return count;
  169. }
  170. protected virtual int Fill (DataTable dataTable, IDbCommand command, CommandBehavior behavior)
  171. {
  172. CommandBehavior commandBehavior = behavior;
  173. // first see that the connection is not close.
  174. if (command.Connection.State == ConnectionState.Closed)
  175. {
  176. command.Connection.Open ();
  177. commandBehavior |= CommandBehavior.CloseConnection;
  178. }
  179. return Fill (dataTable, command.ExecuteReader (commandBehavior));
  180. }
  181. #if NET_1_2
  182. [MonoTODO]
  183. public int Fill (int startRecord, int maxRecords, DataTable[] dataTables)
  184. {
  185. throw new NotImplementedException ();
  186. }
  187. #endif
  188. public int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable)
  189. {
  190. return this.Fill (dataSet, startRecord, maxRecords, srcTable, SelectCommand, CommandBehavior.Default);
  191. }
  192. #if NET_1_2
  193. [MonoTODO]
  194. protected virtual int Fill (DataTable[] dataTables, int startRecord, int maxRecords, IDbCommand command, CommandBehavior behavior)
  195. {
  196. throw new NotImplementedException ();
  197. }
  198. #endif
  199. #if NET_1_2
  200. protected override int Fill (DataSet dataSet, string srcTable, IDataReader dataReader, int startRecord, int maxRecords)
  201. #else
  202. protected virtual int Fill (DataSet dataSet, string srcTable, IDataReader dataReader, int startRecord, int maxRecords)
  203. #endif
  204. {
  205. if (startRecord < 0)
  206. throw new ArgumentException ("The startRecord parameter was less than 0.");
  207. if (maxRecords < 0)
  208. throw new ArgumentException ("The maxRecords parameter was less than 0.");
  209. if (dataReader.FieldCount == 0) {
  210. dataReader.Close ();
  211. return 0;
  212. }
  213. DataTable dataTable;
  214. int resultIndex = 0;
  215. int count = 0;
  216. try {
  217. string tableName = srcTable;
  218. do {
  219. tableName = SetupSchema (SchemaType.Mapped, tableName);
  220. if (tableName != null) {
  221. // check if the table exists in the dataset
  222. if (dataSet.Tables.Contains (tableName))
  223. // get the table from the dataset
  224. dataTable = dataSet.Tables [tableName];
  225. else {
  226. dataTable = new DataTable(tableName);
  227. dataSet.Tables.Add (dataTable);
  228. }
  229. if (!FillTable (dataTable, dataReader, startRecord, maxRecords, ref count))
  230. break;
  231. tableName = String.Format ("{0}{1}", srcTable, ++resultIndex);
  232. startRecord = 0;
  233. maxRecords = 0;
  234. }
  235. } while (dataReader.NextResult ());
  236. } finally {
  237. dataReader.Close ();
  238. }
  239. return count;
  240. }
  241. protected virtual int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior)
  242. {
  243. if (MissingSchemaAction == MissingSchemaAction.AddWithKey)
  244. behavior |= CommandBehavior.KeyInfo;
  245. CommandBehavior commandBehavior = behavior;
  246. if (command.Connection.State == ConnectionState.Closed) {
  247. command.Connection.Open ();
  248. commandBehavior |= CommandBehavior.CloseConnection;
  249. }
  250. return Fill (dataSet, srcTable, command.ExecuteReader (commandBehavior), startRecord, maxRecords);
  251. }
  252. private bool FillTable (DataTable dataTable, IDataReader dataReader, int startRecord, int maxRecords, ref int counter)
  253. {
  254. int counterStart = counter;
  255. int[] mapping = BuildSchema (dataReader, dataTable, SchemaType.Mapped);
  256. object[] readerArray = new object[dataReader.FieldCount];
  257. object[] tableArray = new object[mapping.Length];
  258. while (dataReader.Read () && (maxRecords == 0 || (counterStart - counter) < maxRecords)) {
  259. for (int i = 0; i < startRecord; i++)
  260. dataReader.Read ();
  261. // we get the values from the datareader
  262. dataReader.GetValues (readerArray);
  263. // copy from datareader columns to table columns according to given mapping
  264. for (int i = 0; i < mapping.Length; i++)
  265. tableArray[i] = readerArray[mapping[i]];
  266. try {
  267. dataTable.BeginLoadData ();
  268. dataTable.LoadDataRow (tableArray, AcceptChangesDuringFill);
  269. dataTable.EndLoadData ();
  270. counter++;
  271. } catch (Exception e) {
  272. FillErrorEventArgs args = CreateFillErrorEvent (dataTable, tableArray, e);
  273. OnFillError (args);
  274. if(!args.Continue)
  275. return false;
  276. }
  277. }
  278. return true;
  279. }
  280. public override DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType)
  281. {
  282. return FillSchema (dataSet, schemaType, SelectCommand, DefaultSourceTableName, CommandBehavior.Default);
  283. }
  284. public DataTable FillSchema (DataTable dataTable, SchemaType schemaType)
  285. {
  286. return FillSchema (dataTable, schemaType, SelectCommand, CommandBehavior.Default);
  287. }
  288. public DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, string srcTable)
  289. {
  290. return FillSchema (dataSet, schemaType, SelectCommand, srcTable, CommandBehavior.Default);
  291. }
  292. [MonoTODO ("Verify")]
  293. protected virtual DataTable FillSchema (DataTable dataTable, SchemaType schemaType, IDbCommand command, CommandBehavior behavior)
  294. {
  295. behavior |= CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
  296. if (command.Connection.State == ConnectionState.Closed) {
  297. command.Connection.Open ();
  298. behavior |= CommandBehavior.CloseConnection;
  299. }
  300. IDataReader reader = command.ExecuteReader (behavior);
  301. try
  302. {
  303. string tableName = SetupSchema (schemaType, dataTable.TableName);
  304. if (tableName != null)
  305. {
  306. BuildSchema (reader, dataTable, schemaType);
  307. }
  308. }
  309. finally
  310. {
  311. reader.Close ();
  312. }
  313. return dataTable;
  314. }
  315. [MonoTODO ("Verify")]
  316. protected virtual DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, IDbCommand command, string srcTable, CommandBehavior behavior)
  317. {
  318. behavior |= CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
  319. if (command.Connection.State == ConnectionState.Closed) {
  320. command.Connection.Open ();
  321. behavior |= CommandBehavior.CloseConnection;
  322. }
  323. IDataReader reader = command.ExecuteReader (behavior);
  324. ArrayList output = new ArrayList ();
  325. string tableName = srcTable;
  326. int index = 0;
  327. DataTable table;
  328. try
  329. {
  330. tableName = SetupSchema (schemaType, tableName);
  331. if (tableName != null)
  332. {
  333. if (dataSet.Tables.Contains (tableName))
  334. table = dataSet.Tables [tableName];
  335. else
  336. {
  337. table = new DataTable(tableName);
  338. dataSet.Tables.Add (table);
  339. }
  340. BuildSchema (reader, table, schemaType);
  341. output.Add (table);
  342. tableName = String.Format ("{0}{1}", srcTable, ++index);
  343. }
  344. }
  345. finally
  346. {
  347. reader.Close ();
  348. }
  349. return (DataTable[]) output.ToArray (typeof (DataTable));
  350. }
  351. #if NET_1_2
  352. [MonoTODO]
  353. public DataSet GetDataSet ()
  354. {
  355. throw new NotImplementedException ();
  356. }
  357. [MonoTODO]
  358. public DataTable GetDataTable ()
  359. {
  360. throw new NotImplementedException ();
  361. }
  362. #endif
  363. private string SetupSchema (SchemaType schemaType, string sourceTableName)
  364. {
  365. DataTableMapping tableMapping = null;
  366. if (schemaType == SchemaType.Mapped)
  367. {
  368. tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, sourceTableName, sourceTableName, MissingMappingAction);
  369. if (tableMapping != null)
  370. return tableMapping.DataSetTable;
  371. return null;
  372. }
  373. else
  374. return sourceTableName;
  375. }
  376. [EditorBrowsable (EditorBrowsableState.Advanced)]
  377. public override IDataParameter[] GetFillParameters ()
  378. {
  379. IDataParameter[] parameters = new IDataParameter[SelectCommand.Parameters.Count];
  380. SelectCommand.Parameters.CopyTo (parameters, 0);
  381. return parameters;
  382. }
  383. // this method builds the schema for a given datatable. it returns a int array with
  384. // "array[ordinal of datatable column] == index of source column in data reader".
  385. // each column in the datatable has a mapping to a specific column in the datareader,
  386. // the int array represents this match.
  387. [MonoTODO ("Test")]
  388. private int[] BuildSchema (IDataReader reader, DataTable table, SchemaType schemaType)
  389. {
  390. int readerIndex = 0;
  391. int[] mapping = new int[reader.FieldCount]; // mapping the reader indexes to the datatable indexes
  392. ArrayList primaryKey = new ArrayList ();
  393. ArrayList sourceColumns = new ArrayList ();
  394. foreach (DataRow schemaRow in reader.GetSchemaTable ().Rows) {
  395. // generate a unique column name in the source table.
  396. string sourceColumnName;
  397. if (schemaRow ["ColumnName"].Equals (DBNull.Value))
  398. sourceColumnName = DefaultSourceColumnName;
  399. else
  400. sourceColumnName = (string) schemaRow ["ColumnName"];
  401. string realSourceColumnName = sourceColumnName;
  402. for (int i = 1; sourceColumns.Contains (realSourceColumnName); i += 1)
  403. realSourceColumnName = String.Format ("{0}{1}", sourceColumnName, i);
  404. sourceColumns.Add(realSourceColumnName);
  405. // generate DataSetColumnName from DataTableMapping, if any
  406. string dsColumnName = realSourceColumnName;
  407. DataTableMapping tableMapping = null;
  408. tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, table.TableName, table.TableName, MissingMappingAction);
  409. if (tableMapping != null)
  410. {
  411. table.TableName = tableMapping.DataSetTable;
  412. // check to see if the column mapping exists
  413. DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, realSourceColumnName, MissingMappingAction);
  414. if (columnMapping != null)
  415. {
  416. DataColumn col =
  417. columnMapping.GetDataColumnBySchemaAction(
  418. table ,
  419. (Type)schemaRow["DataType"],
  420. MissingSchemaAction);
  421. if (col != null)
  422. {
  423. // if the column is not in the table - add it.
  424. if (table.Columns.IndexOf(col) == -1)
  425. {
  426. if (MissingSchemaAction == MissingSchemaAction.Add || MissingSchemaAction == MissingSchemaAction.AddWithKey)
  427. table.Columns.Add(col);
  428. }
  429. if (!schemaRow["IsKey"].Equals (DBNull.Value))
  430. if ((bool) (schemaRow ["IsKey"]))
  431. primaryKey.Add (col);
  432. // add the ordinal of the column as a key and the index of the column in the datareader as a value.
  433. mapping[col.Ordinal] = readerIndex;
  434. }
  435. }
  436. }
  437. readerIndex++;
  438. }
  439. if (primaryKey.Count > 0)
  440. table.PrimaryKey = (DataColumn[])(primaryKey.ToArray(typeof (DataColumn)));
  441. return mapping;
  442. }
  443. [MonoTODO]
  444. object ICloneable.Clone ()
  445. {
  446. throw new NotImplementedException ();
  447. }
  448. [MonoTODO]
  449. public int Update (DataRow[] dataRows)
  450. {
  451. if (dataRows == null)
  452. throw new ArgumentNullException("dataRows");
  453. if (dataRows.Length == 0)
  454. return 0;
  455. if (dataRows[0] == null)
  456. throw new ArgumentException("dataRows[0].");
  457. DataTable table = dataRows[0].Table;
  458. if (table == null)
  459. throw new ArgumentException("table is null reference.");
  460. // all rows must be in the same table
  461. for (int i = 0; i < dataRows.Length; i++)
  462. {
  463. if (dataRows[i] == null)
  464. throw new ArgumentException("dataRows[" + i + "].");
  465. if (dataRows[i].Table != table)
  466. throw new ArgumentException(
  467. " DataRow["
  468. + i
  469. + "] is from a different DataTable than DataRow[0].");
  470. }
  471. // get table mapping for this rows
  472. DataTableMapping tableMapping = TableMappings.GetByDataSetTable(table.TableName);
  473. if (tableMapping == null)
  474. {
  475. tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(
  476. TableMappings,
  477. table.TableName,
  478. table.TableName,
  479. MissingMappingAction);
  480. if (tableMapping == null)
  481. tableMapping =
  482. new DataTableMapping(
  483. table.TableName,
  484. table.TableName);
  485. }
  486. DataRow[] copy = new DataRow [dataRows.Length];
  487. Array.Copy(dataRows, 0, copy, 0, dataRows.Length);
  488. return Update(copy, tableMapping);
  489. }
  490. public override int Update (DataSet dataSet)
  491. {
  492. return Update (dataSet, DefaultSourceTableName);
  493. }
  494. public int Update (DataTable dataTable)
  495. {
  496. int index = TableMappings.IndexOfDataSetTable (dataTable.TableName);
  497. if (index < 0)
  498. throw new ArgumentException ();
  499. return Update (dataTable, TableMappings [index]);
  500. }
  501. private int Update (DataTable dataTable, DataTableMapping tableMapping)
  502. {
  503. DataRow[] rows = new DataRow [dataTable.Rows.Count];
  504. dataTable.Rows.CopyTo (rows, 0);
  505. return Update (rows, tableMapping);
  506. }
  507. [MonoTODO]
  508. protected virtual int Update (DataRow[] dataRows, DataTableMapping tableMapping)
  509. {
  510. int updateCount = 0;
  511. foreach (DataRow row in dataRows) {
  512. StatementType statementType = StatementType.Update;
  513. IDbCommand command = null;
  514. string commandName = String.Empty;
  515. bool useCommandBuilder = false;
  516. switch (row.RowState) {
  517. case DataRowState.Added:
  518. statementType = StatementType.Insert;
  519. command = InsertCommand;
  520. commandName = "Insert";
  521. break;
  522. case DataRowState.Deleted:
  523. statementType = StatementType.Delete;
  524. command = DeleteCommand;
  525. commandName = "Delete";
  526. break;
  527. case DataRowState.Modified:
  528. statementType = StatementType.Update;
  529. command = UpdateCommand;
  530. commandName = "Update";
  531. break;
  532. case DataRowState.Unchanged:
  533. continue;
  534. case DataRowState.Detached:
  535. throw new NotImplementedException ();
  536. }
  537. if (command == null)
  538. useCommandBuilder = true;
  539. RowUpdatingEventArgs args = CreateRowUpdatingEvent (row, command, statementType, tableMapping);
  540. OnRowUpdating (args);
  541. if (args.Status == UpdateStatus.ErrorsOccurred)
  542. throw (args.Errors);
  543. if (command == null && args.Command != null)
  544. command = args.Command;
  545. else if (command == null)
  546. throw new InvalidOperationException (String.Format ("Update requires a valid {0}Command when passed a DataRow collection with modified rows.", commandName));
  547. if (!useCommandBuilder) {
  548. DataColumnMappingCollection columnMappings = tableMapping.ColumnMappings;
  549. foreach (IDataParameter parameter in command.Parameters) {
  550. string dsColumnName = parameter.SourceColumn;
  551. if (columnMappings.Contains(parameter.SourceColumn))
  552. dsColumnName = columnMappings [parameter.SourceColumn].DataSetColumn;
  553. DataRowVersion rowVersion = DataRowVersion.Default;
  554. // Parameter version is ignored for non-update commands
  555. if (statementType == StatementType.Update)
  556. rowVersion = parameter.SourceVersion;
  557. if (statementType == StatementType.Delete)
  558. rowVersion = DataRowVersion.Original;
  559. parameter.Value = row [dsColumnName, rowVersion];
  560. }
  561. }
  562. CommandBehavior commandBehavior = CommandBehavior.Default;
  563. if (command.Connection.State == ConnectionState.Closed)
  564. {
  565. command.Connection.Open ();
  566. commandBehavior |= CommandBehavior.CloseConnection;
  567. }
  568. IDataReader reader = null;
  569. try
  570. {
  571. // use ExecuteReader because we want to use the commandbehavior parameter.
  572. // so the connection will be closed if needed.
  573. reader = command.ExecuteReader (commandBehavior);
  574. int tmp = reader.RecordsAffected;
  575. // if the execute does not effect any rows we throw an exception.
  576. if (tmp == 0)
  577. throw new DBConcurrencyException("Concurrency violation: the " + commandName +"Command affected 0 records.");
  578. updateCount += tmp;
  579. OnRowUpdated (CreateRowUpdatedEvent (row, command, statementType, tableMapping));
  580. row.AcceptChanges ();
  581. }
  582. catch (Exception e)
  583. {
  584. if (ContinueUpdateOnError)
  585. row.RowError = e.Message;// do somthing with the error
  586. else
  587. throw e;
  588. }
  589. finally
  590. {
  591. if (reader != null)
  592. reader.Close ();
  593. }
  594. }
  595. return updateCount;
  596. }
  597. public int Update (DataSet dataSet, string sourceTable)
  598. {
  599. MissingMappingAction mappingAction = MissingMappingAction;
  600. if (mappingAction == MissingMappingAction.Ignore)
  601. mappingAction = MissingMappingAction.Error;
  602. DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, sourceTable, sourceTable, mappingAction);
  603. DataTable dataTable = dataSet.Tables[tableMapping.DataSetTable];
  604. if (dataTable == null)
  605. throw new ArgumentException ("sourceTable");
  606. return Update (dataTable, tableMapping);
  607. }
  608. #if ONLY_1_0 || ONLY_1_1
  609. protected virtual void OnFillError (FillErrorEventArgs value)
  610. {
  611. if (FillError != null)
  612. FillError (this, value);
  613. }
  614. #endif
  615. protected abstract void OnRowUpdated (RowUpdatedEventArgs value);
  616. protected abstract void OnRowUpdating (RowUpdatingEventArgs value);
  617. #endregion // Methods
  618. }
  619. }