DataRowCollection.Add 方法

定义

DataRow 中添加一个 DataRowCollectionAdds a DataRow to the DataRowCollection.

重载

Add(DataRow)

将指定的 DataRow 添加到 DataRowCollection 对象中。Adds the specified DataRow to the DataRowCollection object.

Add(Object[])

创建使用指定值的行,并将其添加到 DataRowCollection 中。Creates a row using specified values and adds it to the DataRowCollection.

Add(DataRow)

将指定的 DataRow 添加到 DataRowCollection 对象中。Adds the specified DataRow to the DataRowCollection object.

public:
 void Add(System::Data::DataRow ^ row);
public void Add (System.Data.DataRow row);
member this.Add : System.Data.DataRow -> unit
Public Sub Add (row As DataRow)

参数

row
DataRow

要添加的 DataRowThe DataRow to add.

例外

该行为 null。The row is null.

此行要么属于另一个表,要么已经属于此表。The row either belongs to another table or already belongs to this table.

添加将使约束无效。The addition invalidates a constraint.

添加尝试将 null 置于 DataColumn 为 false 的 AllowDBNull 中。The addition tries to put a null in a DataColumn where AllowDBNull is false.

示例

下面的示例使用 Add 方法将一个新添加 DataRowDataRowCollection 对象。The following example uses the Add method to add a new DataRow to a DataRowCollection object.

private void ShowRows(DataTable table)
{
    // Print the number of rows in the collection.
    Console.WriteLine(table.Rows.Count);
    // Print the value of columns 1 in each row
    foreach(DataRow row in table.Rows)
    {
        Console.WriteLine(row[1]);
    }
}

private void AddRow(DataTable table)
{
    DataRowCollection rowCollection = table.Rows;
    // Instantiate a new row using the NewRow method.

    DataRow newRow = table.NewRow();
    // Insert code to fill the row with values.

    // Add the row to the DataRowCollection.
    table.Rows.Add(newRow);
}
Private Sub ShowRows(Byval table As DataTable)
    ' Print the number of rows in the collection.
    Console.WriteLine(table.Rows.Count)

    Dim row  As DataRow
    ' Print the value of columns 1 in each row
    For Each row In table.Rows
        Console.WriteLine(row(1))
    Next
End Sub
 
Private Sub AddRow(ByVal table As DataTable)
    ' Instantiate a new row using the NewRow method.
    Dim newRow As DataRow = table.NewRow()
    ' Insert code to fill the row with values.

    ' Add the row to the DataRowCollection.
    table.Rows.Add(newRow)
End Sub

注解

若要创建新的 DataRow ,必须使用 NewRow 类的方法 DataTableTo create a new DataRow, you must use the NewRow method of the DataTable class. 使用 NewRow 方法时,将 DataRow 使用父级的架构返回一个新的对象 DataTableWhen you use the NewRow method, a new DataRow object is returned using the schema of parent DataTable. 创建 DataRow 对象并为每个列设置值后,使用 Add 方法将该对象添加到集合中。After you create the DataRow object and set the values for each of its columns, use the Add method to add the object to the collection.

如果用户在事件中生成异常,则会生成异常 RowChangingGenerates an exception if the user generates an exception in the RowChanging event. 如果发生异常,则不会将行添加到表中。If an exception occurs, the row is not added to the table.

另请参阅

适用于

Add(Object[])

创建使用指定值的行,并将其添加到 DataRowCollection 中。Creates a row using specified values and adds it to the DataRowCollection.

public:
 System::Data::DataRow ^ Add(... cli::array <System::Object ^> ^ values);
public:
 virtual System::Data::DataRow ^ Add(cli::array <System::Object ^> ^ values);
public System.Data.DataRow Add (params object?[] values);
public System.Data.DataRow Add (params object[] values);
public virtual System.Data.DataRow Add (object[] values);
member this.Add : obj[] -> System.Data.DataRow
abstract member Add : obj[] -> System.Data.DataRow
override this.Add : obj[] -> System.Data.DataRow
Public Function Add (ParamArray values As Object()) As DataRow
Public Overridable Function Add (values As Object()) As DataRow

参数

values
Object[]

用于创建新行的值的数组。The array of values that are used to create the new row.

返回

DataRow

无。None.

例外

该数组大于表中的列数。The array is larger than the number of columns in the table.

值与其各自的列类型不匹配。A value does not match its respective column type.

添加行会使约束无效。Adding the row invalidates a constraint.

尝试将空值放到 AllowDBNull 为 false 的列中。Trying to put a null in a column where AllowDBNull is false.

示例

下面的示例使用 Add 方法创建一个新的对象并将其添加 DataRowDataRowCollection 中。The following example uses the Add method to create and add a new DataRow object to a DataRowCollection.

private void AddRow(DataTable table)
{
    // Create an array with three elements.
    object[] rowVals = new object[3];
    DataRowCollection rowCollection = table.Rows;
    rowVals[0] = "hello";
    rowVals[1] = "world";
    rowVals[2] = "two";

    // Add and return the new row.
    DataRow row = rowCollection.Add(rowVals);
}
Private Sub AddRow(ByVal table As DataTable)
    ' Create an array with three elements.
    Dim rowVals(2) As Object
    Dim rowCollection As DataRowCollection = table.Rows
    rowVals(0) = "hello"
    rowVals(1) = "world"
    rowVals(2) = "two"

    ' Add and return the new row.
    Dim row As DataRow = rowCollection.Add(rowVals) 
End Sub

注解

如果 DataColumn 对象的 AutoIncrement 设置为 True,应传递 null 以获取该列的默认值。If a DataColumn object has its AutoIncrement set to True, null should be passed to get the default value for that column.

如果在或事件期间生成异常,也会出现异常 ColumnChanging RowChangingExceptions can also occur if you generate an exception during either a ColumnChanging or RowChanging event. 如果发生异常,则不会将行添加到表中。If an exception occurs, the row is not added to the table.

另请参阅

适用于