关键词搜索

源码搜索 ×
×

C# DataRow.ItemArray 属性

发布2014-03-04浏览12315次

详情内容

DataRow.ItemArray 属性
通过一个数组来获取或设置此行的所有值。
命名空间:System.Data

程序集:System.Data(在 system.data.dll 中)

代码示例:

  1. private void CreateRowsWithItemArray()
  2. {
  3. // Make a DataTable using the function below.
  4. DataTable dt = MakeTableWithAutoIncrement();
  5. DataRow relation;
  6. // Declare the array variable.
  7. object [] rowArray = new object[2];
  8. // Create 10 new rows and add to DataRowCollection.
  9. for(int i = 0; i <10; i++)
  10. {
  11. rowArray[0]=null;
  12. rowArray[1]= "item " + i;
  13. relation = dt.NewRow();
  14. relation.ItemArray = rowArray;
  15. dt.Rows.Add(relation);
  16. }
  17. PrintTable(dt);
  18. }
  19. private DataTable MakeTableWithAutoIncrement()
  20. {
  21. // Make a table with one AutoIncrement column.
  22. DataTable table = new DataTable("table");
  23. DataColumn idColumn = new DataColumn("id",
  24. Type.GetType("System.Int32"));
  25. idColumn.AutoIncrement = true;
  26. idColumn.AutoIncrementSeed = 10;
  27. table.Columns.Add(idColumn);
  28. DataColumn firstNameColumn = new DataColumn("Item",
  29. Type.GetType("System.String"));
  30. table.Columns.Add(firstNameColumn);
  31. return table;
  32. }
  33. private void PrintTable(DataTable table)
  34. {
  35. foreach(DataRow row in table.Rows)
  36. {
  37. foreach(DataColumn column in table.Columns)
  38. {
  39. Console.WriteLine(row[column]);
  40. }
  41. }
  42. }
异常:

异常类型 条件

ArgumentException

数组大于表中的列数。

InvalidCastException

数组中的值与其相应的 DataColumn 中的 DataType 不匹配。

ConstraintException

编辑破坏了约束。

ReadOnlyException

编辑试图更改只读列的值。

NoNullAllowedException

编辑试图将空值放在 DataColumn 对象的 AllowDBNull 为 false 的列中。

DeletedRowInaccessibleException

该行已被删除。

DataRow.ItemArray 属性源代码实现:

  1. public object[] ItemArray
  2. {
  3. get
  4. {
  5. int defaultRecord = this.GetDefaultRecord();
  6. object[] array = new object[this._columns.Count];
  7. for (int i = 0; i < array.Length; i++)
  8. {
  9. DataColumn dataColumn = this._columns[i];
  10. array[i] = dataColumn[defaultRecord];
  11. }
  12. return array;
  13. }
  14. set
  15. {
  16. if (value == null)
  17. {
  18. throw ExceptionBuilder.ArgumentNull("ItemArray");
  19. }
  20. if (this._columns.Count < value.Length)
  21. {
  22. throw ExceptionBuilder.ValueArrayLength();
  23. }
  24. DataColumnChangeEventArgs dataColumnChangeEventArgs = null;
  25. if (this._table.NeedColumnChangeEvents)
  26. {
  27. dataColumnChangeEventArgs = new DataColumnChangeEventArgs(this);
  28. }
  29. bool flag = this.BeginEditInternal();
  30. for (int i = 0; i < value.Length; i++)
  31. {
  32. if (value[i] != null)
  33. {
  34. DataColumn dataColumn = this._columns[i];
  35. if (-1L != this.rowID && dataColumn.ReadOnly)
  36. {
  37. throw ExceptionBuilder.ReadOnly(dataColumn.ColumnName);
  38. }
  39. if (dataColumnChangeEventArgs != null)
  40. {
  41. dataColumnChangeEventArgs.InitializeColumnChangeEvent(dataColumn, value[i]);
  42. this._table.OnColumnChanging(dataColumnChangeEventArgs);
  43. }
  44. if (dataColumn.Table != this._table)
  45. {
  46. throw ExceptionBuilder.ColumnNotInTheTable(dataColumn.ColumnName, this._table.TableName);
  47. }
  48. if (-1L != this.rowID && dataColumn.ReadOnly)
  49. {
  50. throw ExceptionBuilder.ReadOnly(dataColumn.ColumnName);
  51. }
  52. if (this.tempRecord == -1)
  53. {
  54. this.BeginEditInternal();
  55. }
  56. object obj = (dataColumnChangeEventArgs != null) ? dataColumnChangeEventArgs.ProposedValue : value[i];
  57. if (obj == null)
  58. {
  59. if (dataColumn.IsValueType)
  60. {
  61. throw ExceptionBuilder.CannotSetToNull(dataColumn);
  62. }
  63. obj = DBNull.Value;
  64. }
  65. try
  66. {
  67. int proposedRecordNo = this.GetProposedRecordNo();
  68. dataColumn[proposedRecordNo] = obj;
  69. }
  70. catch (Exception e)
  71. {
  72. if (ADP.IsCatchableOrSecurityExceptionType(e) && flag)
  73. {
  74. this.CancelEdit();
  75. }
  76. throw;
  77. }
  78. this.LastChangedColumn = dataColumn;
  79. if (dataColumnChangeEventArgs != null)
  80. {
  81. this._table.OnColumnChanged(dataColumnChangeEventArgs);
  82. }
  83. }
  84. }
  85. this.EndEdit();
  86. }
  87. }


相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载