Heim >Backend-Entwicklung >C#.Net-Tutorial >C# DataRow.ItemArray-Typ
DataRow.ItemArray-Eigenschaft
Ruft alle Werte dieser Zeile über ein Array ab oder legt sie fest.
Namespace: System.Data
Assembly: System.Data (in system.data.dll)
Codebeispiel:
private void CreateRowsWithItemArray() { // Make a DataTable using the function below. DataTable dt = MakeTableWithAutoIncrement(); DataRow relation; // Declare the array variable. object [] rowArray = new object[2]; // Create 10 new rows and add to DataRowCollection. for(int i = 0; i <10; i++) { rowArray[0]=null; rowArray[1]= "item " + i; relation = dt.NewRow(); relation.ItemArray = rowArray; dt.Rows.Add(relation); } PrintTable(dt); } private DataTable MakeTableWithAutoIncrement() { // Make a table with one AutoIncrement column. DataTable table = new DataTable("table"); DataColumn idColumn = new DataColumn("id", Type.GetType("System.Int32")); idColumn.AutoIncrement = true; idColumn.AutoIncrementSeed = 10; table.Columns.Add(idColumn); DataColumn firstNameColumn = new DataColumn("Item", Type.GetType("System.String")); table.Columns.Add(firstNameColumn); return table; } private void PrintTable(DataTable table) { foreach(DataRow row in table.Rows) { foreach(DataColumn column in table.Columns) { Console.WriteLine(row[column]); } } }
Ausnahme:
Ausnahmetyp | Bedingung |
---|---|
ArgumentException |
Das Array ist größer als die Anzahl der Spalten in der Tabelle. |
InvalidCastException |
Die Werte im Array stimmen nicht mit dem DataType in ihrer entsprechenden DataColumn überein. |
ConstraintException |
Bearbeiten hat die Einschränkung aufgehoben. |
ReadOnlyException |
Ein Editor hat versucht, den Wert einer schreibgeschützten Spalte zu ändern. |
NoNullAllowedException |
编辑试图将空值放在 DataColumn 对象的 AllowDBNull 为 false 的列中。 |
DeletedRowInaccessibleException |
该行已被删除。 |
DataRow.ItemArray 属性源代码实现:
public object[] ItemArray { get { int defaultRecord = this.GetDefaultRecord(); object[] array = new object[this._columns.Count]; for (int i = 0; i < array.Length; i++) { DataColumn dataColumn = this._columns[i]; array[i] = dataColumn[defaultRecord]; } return array; } set { if (value == null) { throw ExceptionBuilder.ArgumentNull("ItemArray"); } if (this._columns.Count < value.Length) { throw ExceptionBuilder.ValueArrayLength(); } DataColumnChangeEventArgs dataColumnChangeEventArgs = null; if (this._table.NeedColumnChangeEvents) { dataColumnChangeEventArgs = new DataColumnChangeEventArgs(this); } bool flag = this.BeginEditInternal(); for (int i = 0; i < value.Length; i++) { if (value[i] != null) { DataColumn dataColumn = this._columns[i]; if (-1L != this.rowID && dataColumn.ReadOnly) { throw ExceptionBuilder.ReadOnly(dataColumn.ColumnName); } if (dataColumnChangeEventArgs != null) { dataColumnChangeEventArgs.InitializeColumnChangeEvent(dataColumn, value[i]); this._table.OnColumnChanging(dataColumnChangeEventArgs); } if (dataColumn.Table != this._table) { throw ExceptionBuilder.ColumnNotInTheTable(dataColumn.ColumnName, this._table.TableName); } if (-1L != this.rowID && dataColumn.ReadOnly) { throw ExceptionBuilder.ReadOnly(dataColumn.ColumnName); } if (this.tempRecord == -1) { this.BeginEditInternal(); } object obj = (dataColumnChangeEventArgs != null) ? dataColumnChangeEventArgs.ProposedValue : value[i]; if (obj == null) { if (dataColumn.IsValueType) { throw ExceptionBuilder.CannotSetToNull(dataColumn); } obj = DBNull.Value; } try { int proposedRecordNo = this.GetProposedRecordNo(); dataColumn[proposedRecordNo] = obj; } catch (Exception e) { if (ADP.IsCatchableOrSecurityExceptionType(e) && flag) { this.CancelEdit(); } throw; } this.LastChangedColumn = dataColumn; if (dataColumnChangeEventArgs != null) { this._table.OnColumnChanged(dataColumnChangeEventArgs); } } } this.EndEdit(); } }
以上就是C# DataRow.ItemArray 属性的内容,更多相关内容请关注PHP中文网(www.php.cn)!