DbDataAdapter.cs 21 KB

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