DataTable.LoadDataRow 方法

定義

尋找並更新特定資料列。Finds and updates a specific row. 如果找不到符合的資料列,則使用指定值來建立新的資料列。If no matching row is found, a new row is created using the given values.

多載

LoadDataRow(Object[], Boolean)

尋找並更新特定資料列。Finds and updates a specific row. 如果找不到符合的資料列,則使用指定值來建立新的資料列。If no matching row is found, a new row is created using the given values.

LoadDataRow(Object[], LoadOption)

尋找並更新特定資料列。Finds and updates a specific row. 如果找不到符合的資料列,則使用指定值來建立新的資料列。If no matching row is found, a new row is created using the given values.

LoadDataRow(Object[], Boolean)

尋找並更新特定資料列。Finds and updates a specific row. 如果找不到符合的資料列,則使用指定值來建立新的資料列。If no matching row is found, a new row is created using the given values.

public:
 System::Data::DataRow ^ LoadDataRow(cli::array <System::Object ^> ^ values, bool fAcceptChanges);
public System.Data.DataRow LoadDataRow (object[] values, bool fAcceptChanges);
member this.LoadDataRow : obj[] * bool -> System.Data.DataRow
Public Function LoadDataRow (values As Object(), fAcceptChanges As Boolean) As DataRow

參數

values
Object[]

用來建立新資料列的值陣列。An array of values used to create the new row.

fAcceptChanges
Boolean

若要接受變更,則為 true,否則為 falsetrue to accept changes; otherwise false.

傳回

新的 DataRowThe new DataRow.

例外狀況

陣列大於資料表中的資料行數目。The array is larger than the number of columns in the table.

數值不符合其個別的資料行型別。A value doesn't match its respective column type.

加入資料列會使條件約束失效。Adding the row invalidates a constraint.

嘗試將 Null 置於 AllowDBNull 為 False 的資料行中。Attempting to put a null in a column where AllowDBNull is false.

範例

下列範例會使用LoadDataRow方法來嘗試尋找資料列。The following example uses the LoadDataRow method to attempt to find a row. 如果找不到這類資料列,則會使用這些值來建立新的資料列。If no such row is found, the values are used to create a new row.

using System;
using System.Data;

class MyDataSet {
   public static void Main() {
      DataTable dt = new DataTable();

      DataColumn dc1 = new DataColumn("col1");
      DataColumn dc2 = new DataColumn("col2");
      DataColumn dc3 = new DataColumn("col3");

      dt.Columns.Add(dc1);
      dt.Columns.Add(dc2);
      dt.Columns.Add(dc3);

      // Create an array for the values.
      object[] newRow = new object[3];

      // Set the values of the array.
      newRow[0] = "Hello";
      newRow[1] = "World";
      newRow[2] = "two";
      DataRow row;

      dt.BeginLoadData();

      // Add the new row to the rows collection.
      row = dt.LoadDataRow(newRow, true);

      foreach (DataRow dr in dt.Rows) {
         Console.WriteLine(String.Format("Row: {0}, {1}, {2}", dr["col1"], dr["col2"], dr["col3"]));
      }

      dt.EndLoadData();
   }
}
Imports System.Data

Class MyDataSet
   Public Shared Sub Main()
      Dim dt As New DataTable()

      Dim dc1 As New DataColumn("col1")
      Dim dc2 As New DataColumn("col2")
      Dim dc3 As New DataColumn("col3")

      dt.Columns.Add(dc1)
      dt.Columns.Add(dc2)
      dt.Columns.Add(dc3)

      ' Create an array for the values.
      Dim newRow As Object() = New Object(2) {}

      ' Set the values of the array.
      newRow(0) = "Hello"
      newRow(1) = "World"
      newRow(2) = "two"
      Dim row As DataRow

      dt.BeginLoadData()

      ' Add the new row to the rows collection.
      row = dt.LoadDataRow(newRow, True)

      For Each dr As DataRow In dt.Rows
         Console.WriteLine([String].Format("Row: {0}, {1}, {2}", dr("col1"), dr("col2"), dr("col3")))
      Next

      dt.EndLoadData()
   End Sub
End Class

備註

LoadDataRow方法會接受值的陣列,並在主鍵資料行中尋找相符的值。The LoadDataRow method takes an array of values and finds the matching value(s) in the primary key column(s).

如果資料行有預設值,請在陣列中傳遞 null 值,以設定該資料行的預設值。If a column has a default value, pass a null value in the array to set the default value for that column. 同樣地,如果資料行的AutoIncrement屬性設定為 true,請在陣列中傳遞 null 值,以設定資料列的自動產生值。Similarly, if a column has its AutoIncrement property set to true, pass a null value in the array to set the automatically generated value for the row.

true AcceptChanges DataTable如果參數為或未指定,則會加入新的資料,然後呼叫以接受中的所有變更; 如果引數為false,則會將新加入的資料列標記為插入,並將變更為fAcceptChanges現有的資料列會標示為修改。If the fAcceptChanges parameter is true or not specified, the new data is added and then AcceptChanges is called to accept all changes in the DataTable; if the argument is false, newly added rows are marked as insertions, and changes to existing rows are marked as modifications.

例外狀況也可能發生于ColumnChangingRowChanging事件期間。Exceptions can also occur during either a ColumnChanging or RowChanging event. 如果發生例外狀況,則不會將資料列加入資料表中。If an exception occurs, the row is not added to the table.

搭配LoadDataRowBeginLoadData使用。 EndLoadDataUse LoadDataRow in conjunction with BeginLoadData and EndLoadData.

另請參閱

LoadDataRow(Object[], LoadOption)

尋找並更新特定資料列。Finds and updates a specific row. 如果找不到符合的資料列,則使用指定值來建立新的資料列。If no matching row is found, a new row is created using the given values.

public:
 System::Data::DataRow ^ LoadDataRow(cli::array <System::Object ^> ^ values, System::Data::LoadOption loadOption);
public System.Data.DataRow LoadDataRow (object[] values, System.Data.LoadOption loadOption);
member this.LoadDataRow : obj[] * System.Data.LoadOption -> System.Data.DataRow

參數

values
Object[]

用來建立新資料列的值陣列。An array of values used to create the new row.

loadOption
LoadOption

用來判斷陣列值如何套用至現有資料列中對應的值。Used to determine how the array values are applied to the corresponding values in an existing row.

傳回

新的 DataRowThe new DataRow.

備註

LoadDataRow方法會接受值的陣列,並在主鍵資料行中尋找相符的值。The LoadDataRow method takes an array of values and finds the matching value(s) in the primary key column(s).

如果資料行有預設值,請在陣列中傳遞 null 值,以設定該資料行的預設值。If a column has a default value, pass a null value in the array to set the default value for that column. 同樣地,如果資料行的AutoIncrement屬性設定為 true,請在陣列中傳遞 null 值,以設定資料列的自動產生值。Similarly, if a column has its AutoIncrement property set to true, pass a null value in the array to set the automatically generated value for the row.

loadOption參數的值是用來決定如何將陣列中的值套用至現有的資料列。The value of the loadOption parameter is used to determine how the values in the array are applied to an existing row. 例如loadOption ,如果設定為OverwriteChanges,則會將OriginalCurrent個數據行的和值取代為傳入資料列中的值, RowState並將屬性設定Unchanged為。For example, if loadOption is set to OverwriteChanges, the Original and Current values of each column are replaced with the values in the incoming row and the RowState property is set to Unchanged.

例外狀況也可能發生于ColumnChangingRowChanging事件期間。Exceptions can also occur during either a ColumnChanging or RowChanging event. 如果發生例外狀況,則不會將資料列加入資料表中。If an exception occurs, the row is not added to the table.

搭配LoadDataRowBeginLoadData使用。 EndLoadDataUse LoadDataRow in conjunction with BeginLoadData and EndLoadData.

另請參閱

適用於