DataRowCollection.Add 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将一个DataRow添加到DataRowCollection。
重载
| 名称 | 说明 |
|---|---|
| Add(DataRow) |
将指定的 DataRow 对象添加到对象 DataRowCollection 。 |
| Add(Object[]) |
使用指定的值创建一行,并将其添加到该行。DataRowCollection |
Add(DataRow)
- Source:
- DataRowCollection.cs
- Source:
- DataRowCollection.cs
- 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)
参数
例外
行为 null。
该行属于另一个表或已属于此表。
添加会使约束失效。
加法尝试将 DataColumn null 置于 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该方法时,将使用父DataRow对象的架构返回一个新DataTable对象。 创建 DataRow 对象并设置其每个列的值后,请使用 Add 该方法将对象添加到集合中。
如果用户在事件中 RowChanging 生成异常,则生成异常。 如果发生异常,则行不会添加到表中。
另请参阅
适用于
Add(Object[])
- Source:
- DataRowCollection.cs
- Source:
- DataRowCollection.cs
- 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[]
用于创建新行的值数组。
返回
新行。
例外
数组大于表中的列数。
值与其各自的列类型不匹配。
添加行会使约束失效。
尝试在值为 false 的 AllowDBNull 列中放置 null。
示例
以下示例使用 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
注解
如果对象 DataColumn 设置为 AutoIncrement True,则应传递 null 以获取该列的默认值。
如果在某个或ColumnChanging事件期间RowChanging生成异常,也会发生异常。 如果发生异常,则行不会添加到表中。