C# DataRow.ItemArray 屬性

來源:互聯網
上載者:User
DataRow.ItemArray 屬性
通過一個數組來擷取或設定此行的所有值。
命名空間:System.Data

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

程式碼範例:


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]);        }    }}

異常:



異常類型 條件

ArgumentException

數組大於表中的列數。

InvalidCastException

數組中的值與其相應的 DataColumn 中的 DataType 不匹配。

ConstraintException

編輯破壞了約束。

ReadOnlyException

編輯試圖更改唯讀列的值。

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 屬性的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.