DataSet.MergeFailed 事件

当目标和源 DataRow 的主键值相同且 EnforceConstraints 设置为真时发生。

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Event MergeFailed As MergeFailedEventHandler
用法
Dim instance As DataSet
Dim handler As MergeFailedEventHandler

AddHandler instance.MergeFailed, handler
public event MergeFailedEventHandler MergeFailed
public:
event MergeFailedEventHandler^ MergeFailed {
    void add (MergeFailedEventHandler^ value);
    void remove (MergeFailedEventHandler^ value);
}
/** @event */
public void add_MergeFailed (MergeFailedEventHandler value)

/** @event */
public void remove_MergeFailed (MergeFailedEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

有关处理事件的更多信息,请参见 使用事件

示例

下面的示例说明 MergeFailed 事件的用法。

Private Shared Sub DemonstrateMergeFailedEvent()
    ' Create a DataSet with one table containing two columns.
    Dim dataSet AS DataSet = New DataSet("dataSet")
    Dim table As DataTable = New DataTable("Items")
    
    ' Add table to the DataSet.
    dataSet.Tables.Add(table)

    ' Add two columns to the DataTable.
    table.Columns.Add("id", Type.GetType("System.Int32"))
    table.Columns.Add("item", Type.GetType("System.Int32"))

    ' Set the primary key to the first column.
    table.PrimaryKey = new DataColumn() { table.Columns("id") }

    ' Add MergeFailed event handler for the table.
    AddHandler dataSet.MergeFailed, _
        New MergeFailedEventHandler(AddressOf Merge_Failed)

    ' Create a second DataTable identical to the first, 
    Dim t2 As DataTable = table.Clone()

    ' Set the primary key of the new table to the second column.
    ' This will cause the MergeFailed event to be raised when the
    ' table is merged into the DataSet.
    t2.PrimaryKey = New DataColumn() { t2.Columns("item") }
    
    ' Merge table into the DataSet.
    Console.WriteLine("Merging...")
    dataSet.Merge(t2, false, MissingSchemaAction.Add)
End Sub

Private Shared Sub Merge_Failed(sender As object, _
    e As MergeFailedEventArgs)
    Console.WriteLine("Merge_Failed Event: '{0}'", e.Conflict)
End Sub
private static void DemonstrateMergeFailedEvent()
{
    // Create a DataSet with one table containing two columns.
    DataSet dataSet = new DataSet("dataSet");
    DataTable table = new DataTable("Items");

    // Add table to the DataSet.
    dataSet.Tables.Add(table);

    // Add two columns to the DataTable.
    table.Columns.Add("id", typeof(int));
    table.Columns.Add("item", typeof(int));

    // Set the primary key to the first column.
    table.PrimaryKey = new DataColumn[] { table.Columns["id"] };

    // Add MergeFailed event handler for the table.
    dataSet.MergeFailed += new MergeFailedEventHandler(Merge_Failed);

    // Create a second DataTable identical to the first, 
    DataTable t2 = table.Clone();

    // Set the primary key of the new table to the second column.
    // This will cause the MergeFailed event to be raised when the
    // table is merged into the DataSet.
    t2.PrimaryKey = new DataColumn[] { t2.Columns["item"] };

    // Merge the table into the DataSet.
    Console.WriteLine("Merging...");
    dataSet.Merge(t2, false, MissingSchemaAction.Add);
}

private static void Merge_Failed(object sender, MergeFailedEventArgs e)
{
    Console.WriteLine("Merge_Failed Event: '{0}'", e.Conflict);
}

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

DataSet 类
DataSet 成员
System.Data 命名空间

其他资源

在 ADO.NET 中使用 DataSet