DbDataAdapter.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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_2_0
  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_2_0
  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_2_0
  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_2_0
  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_2_0
  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_2_0
  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_2_0
  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_2_0
  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_2_0
  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_2_0
  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. continue;
  232. }
  233. tableName = String.Format ("{0}{1}", srcTable, ++resultIndex);
  234. startRecord = 0;
  235. maxRecords = 0;
  236. }
  237. } while (dataReader.NextResult ());
  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. object[] readerArray = new object[dataReader.FieldCount];
  262. object[] tableArray = new object[mapping.Length];
  263. for (int i = 0; i < startRecord; i++)
  264. dataReader.Read ();
  265. while (dataReader.Read () && (maxRecords == 0 || (counterStart - counter) < maxRecords)) {
  266. // we get the values from the datareader
  267. dataReader.GetValues (readerArray);
  268. // copy from datareader columns to table columns according to given mapping
  269. for (int i = 0; i < mapping.Length; i++)
  270. tableArray[i] = readerArray[mapping[i]];
  271. try {
  272. dataTable.BeginLoadData ();
  273. dataTable.LoadDataRow (tableArray, AcceptChangesDuringFill);
  274. dataTable.EndLoadData ();
  275. counter++;
  276. } catch (Exception e) {
  277. FillErrorEventArgs args = CreateFillErrorEvent (dataTable, tableArray, e);
  278. OnFillError (args);
  279. if(!args.Continue)
  280. return false;
  281. }
  282. }
  283. return true;
  284. }
  285. public override DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType)
  286. {
  287. return FillSchema (dataSet, schemaType, SelectCommand, DefaultSourceTableName, CommandBehavior.Default);
  288. }
  289. public DataTable FillSchema (DataTable dataTable, SchemaType schemaType)
  290. {
  291. return FillSchema (dataTable, schemaType, SelectCommand, CommandBehavior.Default);
  292. }
  293. public DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, string srcTable)
  294. {
  295. return FillSchema (dataSet, schemaType, SelectCommand, srcTable, CommandBehavior.Default);
  296. }
  297. [MonoTODO ("Verify")]
  298. protected virtual DataTable FillSchema (DataTable dataTable, SchemaType schemaType, IDbCommand command, CommandBehavior behavior)
  299. {
  300. behavior |= CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
  301. if (command.Connection.State == ConnectionState.Closed) {
  302. command.Connection.Open ();
  303. behavior |= CommandBehavior.CloseConnection;
  304. }
  305. IDataReader reader = command.ExecuteReader (behavior);
  306. try
  307. {
  308. string tableName = SetupSchema (schemaType, dataTable.TableName);
  309. if (tableName != null)
  310. {
  311. BuildSchema (reader, dataTable, schemaType);
  312. }
  313. }
  314. finally
  315. {
  316. reader.Close ();
  317. }
  318. return dataTable;
  319. }
  320. [MonoTODO ("Verify")]
  321. protected virtual DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, IDbCommand command, string srcTable, CommandBehavior behavior)
  322. {
  323. behavior |= CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
  324. if (command.Connection.State == ConnectionState.Closed) {
  325. command.Connection.Open ();
  326. behavior |= CommandBehavior.CloseConnection;
  327. }
  328. IDataReader reader = command.ExecuteReader (behavior);
  329. ArrayList output = new ArrayList ();
  330. string tableName = srcTable;
  331. int index = 0;
  332. DataTable table;
  333. try
  334. {
  335. tableName = SetupSchema (schemaType, tableName);
  336. if (tableName != null)
  337. {
  338. if (dataSet.Tables.Contains (tableName))
  339. table = dataSet.Tables [tableName];
  340. else
  341. {
  342. table = new DataTable(tableName);
  343. dataSet.Tables.Add (table);
  344. }
  345. BuildSchema (reader, table, schemaType);
  346. output.Add (table);
  347. tableName = String.Format ("{0}{1}", srcTable, ++index);
  348. }
  349. }
  350. finally
  351. {
  352. reader.Close ();
  353. }
  354. return (DataTable[]) output.ToArray (typeof (DataTable));
  355. }
  356. #if NET_2_0
  357. [MonoTODO]
  358. public DataSet GetDataSet ()
  359. {
  360. throw new NotImplementedException ();
  361. }
  362. [MonoTODO]
  363. public DataTable GetDataTable ()
  364. {
  365. throw new NotImplementedException ();
  366. }
  367. #endif
  368. private string SetupSchema (SchemaType schemaType, string sourceTableName)
  369. {
  370. DataTableMapping tableMapping = null;
  371. if (schemaType == SchemaType.Mapped)
  372. {
  373. tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, sourceTableName, sourceTableName, MissingMappingAction);
  374. if (tableMapping != null)
  375. return tableMapping.DataSetTable;
  376. return null;
  377. }
  378. else
  379. return sourceTableName;
  380. }
  381. [EditorBrowsable (EditorBrowsableState.Advanced)]
  382. public override IDataParameter[] GetFillParameters ()
  383. {
  384. IDataParameter[] parameters = new IDataParameter[SelectCommand.Parameters.Count];
  385. SelectCommand.Parameters.CopyTo (parameters, 0);
  386. return parameters;
  387. }
  388. // this method builds the schema for a given datatable. it returns a int array with
  389. // "array[ordinal of datatable column] == index of source column in data reader".
  390. // each column in the datatable has a mapping to a specific column in the datareader,
  391. // the int array represents this match.
  392. [MonoTODO ("Test")]
  393. private int[] BuildSchema (IDataReader reader, DataTable table, SchemaType schemaType)
  394. {
  395. int readerIndex = 0;
  396. int[] mapping = new int[reader.FieldCount]; // mapping the reader indexes to the datatable indexes
  397. ArrayList primaryKey = new ArrayList ();
  398. ArrayList sourceColumns = new ArrayList ();
  399. foreach (DataRow schemaRow in reader.GetSchemaTable ().Rows) {
  400. // generate a unique column name in the source table.
  401. string sourceColumnName;
  402. if (schemaRow ["ColumnName"].Equals (DBNull.Value))
  403. sourceColumnName = DefaultSourceColumnName;
  404. else
  405. sourceColumnName = (string) schemaRow ["ColumnName"];
  406. string realSourceColumnName = sourceColumnName;
  407. for (int i = 1; sourceColumns.Contains (realSourceColumnName); i += 1)
  408. realSourceColumnName = String.Format ("{0}{1}", sourceColumnName, i);
  409. sourceColumns.Add(realSourceColumnName);
  410. // generate DataSetColumnName from DataTableMapping, if any
  411. string dsColumnName = realSourceColumnName;
  412. DataTableMapping tableMapping = null;
  413. tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, table.TableName, table.TableName, MissingMappingAction);
  414. if (tableMapping != null)
  415. {
  416. table.TableName = tableMapping.DataSetTable;
  417. // check to see if the column mapping exists
  418. DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, realSourceColumnName, MissingMappingAction);
  419. if (columnMapping != null)
  420. {
  421. DataColumn col =
  422. columnMapping.GetDataColumnBySchemaAction(
  423. table ,
  424. (Type)schemaRow["DataType"],
  425. MissingSchemaAction);
  426. if (col != null)
  427. {
  428. // if the column is not in the table - add it.
  429. if (table.Columns.IndexOf(col) == -1)
  430. {
  431. if (MissingSchemaAction == MissingSchemaAction.Add || MissingSchemaAction == MissingSchemaAction.AddWithKey)
  432. table.Columns.Add(col);
  433. }
  434. if (!schemaRow["IsKey"].Equals (DBNull.Value))
  435. if ((bool) (schemaRow ["IsKey"]))
  436. primaryKey.Add (col);
  437. // add the ordinal of the column as a key and the index of the column in the datareader as a value.
  438. mapping[col.Ordinal] = readerIndex;
  439. }
  440. }
  441. }
  442. readerIndex++;
  443. }
  444. if (primaryKey.Count > 0)
  445. table.PrimaryKey = (DataColumn[])(primaryKey.ToArray(typeof (DataColumn)));
  446. return mapping;
  447. }
  448. [MonoTODO]
  449. object ICloneable.Clone ()
  450. {
  451. throw new NotImplementedException ();
  452. }
  453. [MonoTODO]
  454. public int Update (DataRow[] dataRows)
  455. {
  456. if (dataRows == null)
  457. throw new ArgumentNullException("dataRows");
  458. if (dataRows.Length == 0)
  459. return 0;
  460. if (dataRows[0] == null)
  461. throw new ArgumentException("dataRows[0].");
  462. DataTable table = dataRows[0].Table;
  463. if (table == null)
  464. throw new ArgumentException("table is null reference.");
  465. // all rows must be in the same table
  466. for (int i = 0; i < dataRows.Length; i++)
  467. {
  468. if (dataRows[i] == null)
  469. throw new ArgumentException("dataRows[" + i + "].");
  470. if (dataRows[i].Table != table)
  471. throw new ArgumentException(
  472. " DataRow["
  473. + i
  474. + "] is from a different DataTable than DataRow[0].");
  475. }
  476. // get table mapping for this rows
  477. DataTableMapping tableMapping = TableMappings.GetByDataSetTable(table.TableName);
  478. if (tableMapping == null)
  479. {
  480. tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(
  481. TableMappings,
  482. table.TableName,
  483. table.TableName,
  484. MissingMappingAction);
  485. if (tableMapping == null)
  486. tableMapping =
  487. new DataTableMapping(
  488. table.TableName,
  489. table.TableName);
  490. }
  491. DataRow[] copy = new DataRow [dataRows.Length];
  492. Array.Copy(dataRows, 0, copy, 0, dataRows.Length);
  493. return Update(copy, tableMapping);
  494. }
  495. public override int Update (DataSet dataSet)
  496. {
  497. return Update (dataSet, DefaultSourceTableName);
  498. }
  499. public int Update (DataTable dataTable)
  500. {
  501. int index = TableMappings.IndexOfDataSetTable (dataTable.TableName);
  502. if (index < 0)
  503. throw new ArgumentException ();
  504. return Update (dataTable, TableMappings [index]);
  505. }
  506. private int Update (DataTable dataTable, DataTableMapping tableMapping)
  507. {
  508. DataRow[] rows = new DataRow [dataTable.Rows.Count];
  509. dataTable.Rows.CopyTo (rows, 0);
  510. return Update (rows, tableMapping);
  511. }
  512. [MonoTODO]
  513. protected virtual int Update (DataRow[] dataRows, DataTableMapping tableMapping)
  514. {
  515. int updateCount = 0;
  516. foreach (DataRow row in dataRows) {
  517. StatementType statementType = StatementType.Update;
  518. IDbCommand command = null;
  519. string commandName = String.Empty;
  520. bool useCommandBuilder = false;
  521. switch (row.RowState) {
  522. case DataRowState.Added:
  523. statementType = StatementType.Insert;
  524. command = InsertCommand;
  525. commandName = "Insert";
  526. break;
  527. case DataRowState.Deleted:
  528. statementType = StatementType.Delete;
  529. command = DeleteCommand;
  530. commandName = "Delete";
  531. break;
  532. case DataRowState.Modified:
  533. statementType = StatementType.Update;
  534. command = UpdateCommand;
  535. commandName = "Update";
  536. break;
  537. case DataRowState.Unchanged:
  538. continue;
  539. case DataRowState.Detached:
  540. throw new NotImplementedException ();
  541. }
  542. if (command == null)
  543. useCommandBuilder = true;
  544. RowUpdatingEventArgs args = CreateRowUpdatingEvent (row, command, statementType, tableMapping);
  545. OnRowUpdating (args);
  546. if (args.Status == UpdateStatus.ErrorsOccurred)
  547. throw (args.Errors);
  548. if (command == null && args.Command != null)
  549. command = args.Command;
  550. else if (command == null)
  551. throw new InvalidOperationException (String.Format ("Update requires a valid {0}Command when passed a DataRow collection with modified rows.", commandName));
  552. if (!useCommandBuilder) {
  553. DataColumnMappingCollection columnMappings = tableMapping.ColumnMappings;
  554. foreach (IDataParameter parameter in command.Parameters) {
  555. string dsColumnName = parameter.SourceColumn;
  556. if (columnMappings.Contains(parameter.SourceColumn))
  557. dsColumnName = columnMappings [parameter.SourceColumn].DataSetColumn;
  558. DataRowVersion rowVersion = DataRowVersion.Default;
  559. // Parameter version is ignored for non-update commands
  560. if (statementType == StatementType.Update)
  561. rowVersion = parameter.SourceVersion;
  562. if (statementType == StatementType.Delete)
  563. rowVersion = DataRowVersion.Original;
  564. parameter.Value = row [dsColumnName, rowVersion];
  565. }
  566. }
  567. CommandBehavior commandBehavior = CommandBehavior.Default;
  568. if (command.Connection.State == ConnectionState.Closed)
  569. {
  570. command.Connection.Open ();
  571. commandBehavior |= CommandBehavior.CloseConnection;
  572. }
  573. IDataReader reader = null;
  574. try
  575. {
  576. // use ExecuteReader because we want to use the commandbehavior parameter.
  577. // so the connection will be closed if needed.
  578. reader = command.ExecuteReader (commandBehavior);
  579. int tmp = reader.RecordsAffected;
  580. // if the execute does not effect any rows we throw an exception.
  581. if (tmp == 0)
  582. throw new DBConcurrencyException("Concurrency violation: the " + commandName +"Command affected 0 records.");
  583. updateCount += tmp;
  584. OnRowUpdated (CreateRowUpdatedEvent (row, command, statementType, tableMapping));
  585. row.AcceptChanges ();
  586. }
  587. catch (Exception e)
  588. {
  589. if (ContinueUpdateOnError)
  590. row.RowError = e.Message;// do somthing with the error
  591. else
  592. throw e;
  593. }
  594. finally
  595. {
  596. if (reader != null)
  597. reader.Close ();
  598. }
  599. }
  600. return updateCount;
  601. }
  602. public int Update (DataSet dataSet, string sourceTable)
  603. {
  604. MissingMappingAction mappingAction = MissingMappingAction;
  605. if (mappingAction == MissingMappingAction.Ignore)
  606. mappingAction = MissingMappingAction.Error;
  607. DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, sourceTable, sourceTable, mappingAction);
  608. DataTable dataTable = dataSet.Tables[tableMapping.DataSetTable];
  609. if (dataTable == null)
  610. throw new ArgumentException ("sourceTable");
  611. return Update (dataTable, tableMapping);
  612. }
  613. #if ONLY_1_0 || ONLY_1_1
  614. protected virtual void OnFillError (FillErrorEventArgs value)
  615. {
  616. if (FillError != null)
  617. FillError (this, value);
  618. }
  619. #endif
  620. protected abstract void OnRowUpdated (RowUpdatedEventArgs value);
  621. protected abstract void OnRowUpdating (RowUpdatingEventArgs value);
  622. #endregion // Methods
  623. }
  624. }