运行应用程序时,它会复制数据库行(大约5次)

时间:2017-12-14 15:05:43

标签: c# wpf ms-access

这是目前测试应用程序链接到测试数据库。该数据库目前有5条记录,并以Access(accdb格式)存储。在Visual Studio中打开解决方案(未运行)时,它会复制现有行(总共35行)。如果我关闭解决方案并再次打开它,那么我有155行,依此类推。

MainWindows.xaml

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <!--create an instance of our DataProvider class-->
    <ObjectDataProvider x:Key="CustomerDataProvider"
                        ObjectType="{x:Type local:CustomerDataProvider}"/>
    <!--define the method which is invoked to obtain our data-->
    <ObjectDataProvider x:Key="Customer"
                        ObjectInstance="{StaticResource 
CustomerDataProvider}"
                        MethodName="GetCustomers"/>
</Window.Resources>
<DockPanel DataContext="{Binding Source={StaticResource Customer}}">
    <DataGrid ItemsSource="{Binding}" Name="dataGrid" />
</DockPanel>
</Window>
Class
using System.Data;
using WpfApp1.Database5DataSetTableAdapters;

namespace WpfApp1
{
public class Database5Provider
{
    private static Database5DataSet database5DataSet;

    public static Database5DataSet Database5DataSet
    {
        get
        {
            if (database5DataSet == null)
            {
                database5DataSet = new Database5DataSet();
            }
            return Database5Provider.database5DataSet;
        }
    }
}

public class CustomerDataProvider
{
    private CustomerTableAdapter adapter;

    // private Database5DataSet dataset; 

    public CustomerDataProvider()
    {
        Database5DataSet dataset = Database5Provider.Database5DataSet;

        adapter = new CustomerTableAdapter();
        adapter.Fill(Database5Provider.Database5DataSet.Customer);

        dataset.Customer.CustomerRowChanged += new Database5DataSet.CustomerRowChangeEventHandler(CustomerRowModified);
        dataset.Customer.CustomerRowDeleted += new Database5DataSet.CustomerRowChangeEventHandler(CustomerRowModified);
    }


    void CustomerRowModified(object sender, Database5DataSet.CustomerRowChangeEvent e)
    {
        adapter.Update(Database5Provider.Database5DataSet.Customer);
    }

    public DataView GetCustomers()
    {
        return Database5Provider.Database5DataSet.Customer.DefaultView;
    }
}
}

我可以看到MainWindows.xaml设计视图加载时发生重复。虽然它说“加载设计师......你可以在设计师在后台加载时继续工作。”没有重复发生。

任何帮助都将不胜感激。

由于

菲尔

0 个答案:

没有答案