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.

如果 fAcceptChanges 参数 true 或未指定,则会添加新数据,然后调用 AcceptChanges,以接受 DataTable中的所有更改;如果 false参数,新添加的行将被标记为插入,对现有行所做的更改将被标记为修改。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.

LoadDataRowBeginLoadDataEndLoadData结合使用。Use 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 属性将设置为 UnchangedFor 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.

LoadDataRowBeginLoadDataEndLoadData结合使用。Use LoadDataRow in conjunction with BeginLoadData and EndLoadData.

另请参阅

适用于