DataRowCollection.Add 方法

定义

DataRow 中添加一个 DataRowCollection

重载

Add(DataRow)

将指定的 DataRow 添加到 DataRowCollection 对象中。

Add(Object[])

创建使用指定值的行,并将其添加到 DataRowCollection 中。

Add(DataRow)

Source:
DataRowCollection.cs
Source:
DataRowCollection.cs
Source:
DataRowCollection.cs

将指定的 DataRow 添加到 DataRowCollection 对象中。

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

要添加的 DataRow

例外

该行为 null。

此行要么属于另一个表,要么已经属于此表。

添加将使约束无效。

添加尝试将 null 置于 DataColumn 为 false 的 AllowDBNull 中。

示例

以下示例使用 Add 方法向 对象添加新DataRowDataRowCollection的 。

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 类的 DataTable 方法。 使用 NewRow 方法时,将使用父 DataTable的架构返回一个新DataRow对象。 创建 DataRow 对象并设置其每个列的值后,使用 Add 方法将 对象添加到集合。

如果用户在 事件中 RowChanging 生成异常,则生成异常。 如果发生异常,则不会将该行添加到表中。

另请参阅

适用于

Add(Object[])

Source:
DataRowCollection.cs
Source:
DataRowCollection.cs
Source:
DataRowCollection.cs

创建使用指定值的行,并将其添加到 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[]

用于创建新行的值的数组。

返回

新行。

例外

该数组大于表中的列数。

值与其各自的列类型不匹配。

添加行会使约束无效。

尝试将空值放到 AllowDBNull 为 false 的列中。

示例

以下示例使用 Add 方法创建一个新 DataRow 对象 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

注解

如果对象DataColumnAutoIncrement设置为 True,则应传递 null 以获取该列的默认值。

如果在 或 RowChanging 事件期间生成异常,ColumnChanging也可能发生异常。 如果发生异常,则不会将该行添加到表中。

另请参阅

适用于