Share via


ListViewInsertedEventArgs.AffectedRows 属性

定义

获取受插入操作影响的行数。

public:
 property int AffectedRows { int get(); };
public int AffectedRows { get; }
member this.AffectedRows : int
Public ReadOnly Property AffectedRows As Integer

属性值

受插入操作影响的行数。

示例

下面的示例演示如何使用 ListViewInsertedEventArgs 传递给 事件的处理程序 ItemInserted 的对象来确定插入操作期间是否引发了异常。 此代码示例是为 ListViewInsertedEventArgs 类提供的一个更大示例的一部分。

void ContactsListView_ItemInserted(Object sender, ListViewInsertedEventArgs e)
{
  if (e.Exception != null)
  {
    if (e.AffectedRows == 0)
    {
      e.KeepInInsertMode = true;
      Message.Text = "An exception occurred inserting the new Contact. " +
        "Please verify your values and try again.";
    }
    else
      Message.Text = "An exception occurred inserting the new Contact. " +
        "Please verify the values in the newly inserted item.";

    e.ExceptionHandled = true;
  }
}
Sub ContactsListView_ItemInserted(ByVal sender As Object, ByVal e As ListViewInsertedEventArgs)

  If e.Exception IsNot Nothing Then

    If e.AffectedRows = 0 Then
      e.KeepInInsertMode = True
      Message.Text = "An exception occurred inserting the new Contact. " & _
        "Please verify your values and try again."
    Else
      Message.Text = "An exception occurred inserting the new Contact. " & _
        "Please verify the values in the newly inserted item."
    End If

    e.ExceptionHandled = True
  End If
End Sub

注解

AffectedRows使用 属性可确定数据源控件返回的插入操作影响的记录数。 此属性通常用于以下方案:

  • 验证是否只插入了一条记录。 有时,错误写入的 insert 语句可能会插入多条记录。

  • 验证是否未插入记录,因为插入操作期间发生错误,但未引发异常。

  • 若要验证在支持冲突检测的数据源控件 (乐观并发) 其 ConflictDetection 属性设置为 ConflictOptions.CompareAllValues 枚举值时是否插入了记录。 支持此属性的控件包括 SqlDataSourceObjectDataSource 控件。 当 属性具有此值时,如果记录的键值与其他用户插入的记录相同,则可能不会插入该记录。

适用于

另请参阅