DbDataAdapter.cs 8.1 KB

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