DataTable.LoadDataRow 方法

.NET Framework 类库 

DataTable.LoadDataRow 方法

查找和更新特定行。如果找不到任何匹配行,则使用给定值创建新行。

[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,则将新添加的行标记为插入,并将现有行的更改标记为修改。

ColumnChangingRowChanging 事件过程中还可能出现异常。如果发生异常,则不向表中添加行。

LoadDataRowBeginLoadDataEndLoadData 一起使用。

示例

[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 精简版

请参见

DataTable 类 | DataTable 成员 | System.Data 命名空间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值