DbDataAdapter.cs 22 KB

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