DbDataAdapter.cs 21 KB

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