DbDataAdapter.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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) 2002 Tim Coleman
  10. //
  11. using System.Data;
  12. namespace System.Data.Common
  13. {
  14. /// <summary>
  15. /// Aids implementation of the IDbDataAdapter interface. Inheritors of DbDataAdapter implement a set of functions to provide strong typing, but inherit most of the functionality needed to fully implement a DataAdapter.
  16. /// </summary>
  17. public abstract class DbDataAdapter : DataAdapter, ICloneable
  18. {
  19. #region Fields
  20. public const string DefaultSourceTableName = "Table";
  21. protected IDbCommand selectCommand;
  22. protected IDbCommand insertCommand;
  23. protected IDbCommand deleteCommand;
  24. protected IDbCommand updateCommand;
  25. #endregion
  26. #region Constructors
  27. protected DbDataAdapter()
  28. {
  29. }
  30. #endregion
  31. #region Properties
  32. public IDbCommand SelectCommand {
  33. get { return selectCommand; }
  34. set { selectCommand = value; }
  35. }
  36. public IDbCommand InsertCommand {
  37. get { return insertCommand; }
  38. set { insertCommand = value; }
  39. }
  40. public IDbCommand DeleteCommand {
  41. get { return deleteCommand; }
  42. set { deleteCommand = value; }
  43. }
  44. public IDbCommand UpdateCommand {
  45. get { return updateCommand; }
  46. set { updateCommand = value; }
  47. }
  48. #endregion
  49. #region Methods
  50. public override int Fill (DataSet dataSet)
  51. {
  52. return Fill (dataSet, DefaultSourceTableName);
  53. }
  54. public int Fill (DataTable dataTable)
  55. {
  56. return Fill (dataTable.DataSet, dataTable.TableName);
  57. }
  58. public int Fill (DataSet dataSet, string srcTable)
  59. {
  60. return Fill (dataSet, 0, 0, srcTable);
  61. }
  62. protected virtual int Fill (DataTable dataTable, IDataReader dataReader)
  63. {
  64. return Fill (dataTable.DataSet, dataTable.TableName, dataReader, 0, 0);
  65. }
  66. protected virtual int Fill (DataTable dataTable, IDbCommand command, CommandBehavior behavior)
  67. {
  68. return Fill (dataTable.DataSet, 0, 0, dataTable.TableName, command, behavior);
  69. }
  70. public int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable)
  71. {
  72. return this.Fill (dataSet, startRecord, maxRecords, srcTable, selectCommand, CommandBehavior.Default);
  73. }
  74. protected virtual int Fill (DataSet dataSet, string srcTable, IDataReader dataReader, int startRecord, int maxRecords)
  75. {
  76. DataTable table;
  77. int changeCount = 0;
  78. string tableName = srcTable;
  79. int i = 0;
  80. if (startRecord < 0)
  81. throw new ArgumentException ("The startRecord parameter was less than 0.");
  82. if (maxRecords < 0)
  83. throw new ArgumentException ("The maxRecords parameter was less than 0.");
  84. do
  85. {
  86. if (dataSet.Tables.Contains (tableName))
  87. {
  88. table = dataSet.Tables[tableName];
  89. }
  90. else // create a new table
  91. {
  92. table = new DataTable (tableName);
  93. for (int j = 0; j < dataReader.FieldCount; j += 1)
  94. {
  95. string baseColumnName = dataReader.GetName (j);
  96. string columnName = "";
  97. if (baseColumnName == "")
  98. baseColumnName = "Column";
  99. else
  100. columnName = baseColumnName;
  101. for (int k = 1; table.Columns.Contains (columnName) || columnName == ""; k += 1)
  102. columnName = String.Format ("{0}{1}", baseColumnName, k);
  103. table.Columns.Add (new DataColumn (columnName, dataReader.GetFieldType (j)));
  104. }
  105. dataSet.Tables.Add (table);
  106. }
  107. DataRow row;
  108. object[] itemArray = new object[dataReader.FieldCount];
  109. // limit results for first results only.
  110. if (srcTable == tableName)
  111. for (int k = 0; k < startRecord; k += 1)
  112. dataReader.Read ();
  113. while (dataReader.Read () && !(maxRecords > 0 && changeCount >= maxRecords && srcTable == tableName))
  114. {
  115. // need to check for existing rows to reconcile if we have key
  116. // information. skip this step for now
  117. // append rows to the end of the current table.
  118. dataReader.GetValues (itemArray);
  119. row = table.Rows.Add (itemArray);
  120. if (AcceptChangesDuringFill)
  121. row.AcceptChanges ();
  122. changeCount += 1;
  123. }
  124. i += 1;
  125. tableName = String.Format ("{0}{1}", srcTable, i);
  126. } while (dataReader.NextResult ());
  127. dataReader.Close ();
  128. return changeCount;
  129. }
  130. protected virtual int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior)
  131. {
  132. if (command.Connection.State == ConnectionState.Closed)
  133. {
  134. command.Connection.Open ();
  135. behavior |= CommandBehavior.CloseConnection;
  136. }
  137. return this.Fill (dataSet, srcTable, command.ExecuteReader (behavior), startRecord, maxRecords);
  138. }
  139. [MonoTODO]
  140. public override DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType)
  141. {
  142. throw new NotImplementedException ();
  143. }
  144. [MonoTODO]
  145. public DataTable FillSchema (DataTable dataTable, SchemaType schemaType)
  146. {
  147. throw new NotImplementedException ();
  148. }
  149. [MonoTODO]
  150. public DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, string srcTable)
  151. {
  152. throw new NotImplementedException ();
  153. }
  154. [MonoTODO]
  155. protected virtual DataTable FillSchema (DataTable dataTable, SchemaType schemaType, IDbCommand command, CommandBehavior behavior)
  156. {
  157. throw new NotImplementedException ();
  158. }
  159. [MonoTODO]
  160. protected virtual DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, IDbCommand command, string srcTable, CommandBehavior behavior)
  161. {
  162. throw new NotImplementedException ();
  163. }
  164. [MonoTODO]
  165. public override IDataParameter[] GetFillParameters ()
  166. {
  167. throw new NotImplementedException ();
  168. }
  169. [MonoTODO]
  170. public int Update (DataRow[] dataRows)
  171. {
  172. throw new NotImplementedException ();
  173. }
  174. [MonoTODO]
  175. public override int Update (DataSet ds)
  176. {
  177. throw new NotImplementedException ();
  178. }
  179. [MonoTODO]
  180. public int Update (DataTable dt)
  181. {
  182. throw new NotImplementedException ();
  183. }
  184. [MonoTODO]
  185. protected virtual int Update (DataRow[] row, DataTableMapping dtm)
  186. {
  187. throw new NotImplementedException ();
  188. }
  189. [MonoTODO]
  190. public int Update (DataSet ds, string s)
  191. {
  192. throw new NotImplementedException ();
  193. }
  194. protected abstract RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping);
  195. protected abstract RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping);
  196. [MonoTODO]
  197. protected virtual void OnFillError (FillErrorEventArgs value)
  198. {
  199. throw new NotImplementedException ();
  200. }
  201. protected abstract void OnRowUpdated (RowUpdatedEventArgs value);
  202. protected abstract void OnRowUpdating (RowUpdatingEventArgs value);
  203. public event FillErrorEventHandler FillError;
  204. [MonoTODO]
  205. public object Clone ()
  206. {
  207. throw new NotImplementedException ();
  208. }
  209. #endregion
  210. }
  211. }