.NET Framework 类库 |
查找和更新特定行。如果找不到任何匹配行,则使用给定值创建新行。
[Visual Basic] Public Function LoadDataRow( _ ByVal values() As Object, _ ByVal fAcceptChanges As Boolean _ ) As DataRow [C#] public DataRow LoadDataRow( object[] values, bool fAcceptChanges ); [C++] public: DataRow* LoadDataRow( Object* values __gc[], bool fAcceptChanges ); [JScript] public function LoadDataRow( values : Object[], fAcceptChanges : Boolean ) : DataRow;
参数
-
values
- 用于创建新行的值的数组。 fAcceptChanges
- 如果接受更改,则为 true;否则为 false。
返回值
新的 DataRow。
异常
异常类型 | 条件 |
---|---|
ArgumentException | 数组大于表中的列数。 |
InvalidCastException | 值与其各自的列类型不匹配。 |
ConstraintException | 添加行将使约束无效。 |
NoNullAllowedException | 试图将空值放到 AllowDBNull 为假的列中。 |
备注
LoadDataRow 方法提取值的数组,并在主键列中查找匹配值。
如果某一列具有默认值,则在数组中传递空值,以便为该列设置默认值。同样,如果列的 AutoIncrement 属性设置为真,则在数组中传递空值,以便为行设置自动生成的值。
如果 fAcceptChanges 参数为 true 或未指定,则添加新数据,然后调用 AcceptChanges 以接受 DataTable 中的所有更改;如果该参数为 false,则将新添加的行标记为插入,并将现有行的更改标记为修改。
在 ColumnChanging 或 RowChanging 事件过程中还可能出现异常。如果发生异常,则不向表中添加行。
将 LoadDataRow 与 BeginLoadData 和 EndLoadData 一起使用。
示例
[Visual Basic, C#, C++] 以下示例使用 LoadDataRow 方法尝试查找行。如果找不到这样的行,这些值将用于创建新行。
[Visual Basic] Private Sub MyAddUpdate(ByVal myTable As DataTable) ' Create an array for the values. Dim newRow(2) As Object ' Set the values of the array. newRow(0) = "Hello" newRow(1) = "World" newRow(2) = "two" Dim myRow As DataRow myTable.BeginLoadData() ' Add the new row to the rows collection. myRow = myTable.LoadDataRow(newRow, True) myTable.EndLoadData() End Sub [C#] private void MyAddUpdate(DataTable myTable){ // 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 myRow; myTable.BeginLoadData(); // Add the new row to the rows collection. myRow = myTable.LoadDataRow(newRow, true); myTable.EndLoadData(); } [C++] private: void MyAddUpdate(DataTable* myTable){ // Create an array for the values. Object* newRow[] = new Object*[3]; // Set the values of the array. newRow[0] = S"Hello"; newRow[1] = S"World"; newRow[2] = S"two"; DataRow* myRow; myTable->BeginLoadData(); // Add the new row to the rows collection. myRow = myTable->LoadDataRow(newRow, true); myTable->EndLoadData(); }
[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 。
要求
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版