C# DataRow.ItemArray 屬性
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 不符。 |
*&*編輯破壞了約束。 | |
編輯試圖更改唯讀列的值。 | 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)!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

可以採用多種方法修改 XML 格式:使用文本編輯器(如 Notepad )進行手工編輯;使用在線或桌面 XML 格式化工具(如 XMLbeautifier)進行自動格式化;使用 XML 轉換工具(如 XSLT)定義轉換規則;或者使用編程語言(如 Python)進行解析和操作。修改時需謹慎,並備份原始文件。
