带有Dapper C#的SQLite-Windows窗体:使用ComboBox Lisl属性使用外键检索数据库数据

问题描述

我在Visual Studio 2019中使用C#。我的数据库使用Dapper在sqlite中。

这就是我正在努力的目标。

我的数据库中有2个已连接的表。 父级tbClient。子表tbProject。

tbProject有一个ClientId字段。

我使用ComboBox将数据从数据库WireUpp到表单。我有一个用于Client的表单,一个用于Project的表单,在此表单中,我在ComboBox中选择了一个CLient,并将其ID保存在我的tbProjet中。

这个想法很简单,但是我很挣扎,因为我使用的是Windows(WPF)的示例,而我的应用程序是Windows Forms。我注意到ComboBox属性不相同,所以当我想打开特定客户端的项目时,在访问正确的项目字段时遇到了一些麻烦。

让我们展示如何在WPF应用程序和WinForm应用程序中完成它。我想获得帮助会更加清晰。

下面是项目表单的代码:第一个是WPF应用程序,第二个是WinForm,在这里我还无法完成工作。

WPF应用程序:

 // WPF Application:
 namespace MyApp.Controls
 {  
    public ProjectControls()
    {
        InitializeComponent();
        InitializeClientList();
        WireUpDropDowns();
    }

    private void WireUpDropDowns()
    {
        clientDropDown.ItemsSource = clients;
        clientDropDown.displayMemberPath = "Name";
        clientDropDown.SelectedValuePath = "Id";

        projectDropDown.ItemsSource = projects;
        projectDropDown.displayMemberPath = "displayValue";
        projectDropDown.SelectedValuePath = "Id";
    }

    private void InitializeClientList()
    {
        string sql = "select * from Client order by Name";
        var clientList = sqliteDataAccess.LoadData<ClientModel>(sql,new Dictionary<string,object>());
        clientList.ForEach(x => clients.Add(x));
    }

     private void clientDropDown_SelectionChanged(object sender,SelectionChangedEventArgs e)
    {
        LoadProjectDropDown();
    }
    private void LoadProjectDropDown()
    {
        string sql = "select * from tbProject where ClientId = @ClientId";

        Dictionary<string,object> parameters = new Dictionary<string,object>
        {
            { "@ClientId",clientDropDown.SelectedValue }
        };

        var records = sqliteDataAccess.LoadData<ProjectsModel>(sql,parameters);

        projects.Clear();
        records.ForEach(x => projects.Add(x));
    }
}

Windows窗体应用程序:

     // Windows Forms Application:
 namespace MyApp.Controls
 {  
    public ProjectControls()
    {
        InitializeComponent();
        InitializeClientList();
        WireUpDropDowns();
    }

    private void WireUpDropDowns()
    {
        clientDropDown.DataSource = null;
        clientDropDown.DataSource = clients;
        clientDropDown.displayMember= "Name";
        clientDropDown.ValueMember = "Id";

        projectDropDown.DataSource = null;
        projectDropDown.DataSource= projects;
        projectDropDown.displayMember = "displayValue";
        projectDropDown.ValueMember= "Id";
    }

    private void InitializeClientList()
    {
        string sql = "select * from Client order by Name";
        var clientList = sqliteDataAccess.LoadData<ClientModel>(sql,clientDropDown.SelectedValue } 
         // --> I think the problem is here,I am passing an object to the database where ClientId is an integer type. I tried to use Selectedindex instead,but with this property,I do not get the correct Project from the table
        };

        var records = sqliteDataAccess.LoadData<ProjectsModel>(sql,parameters);

        projects.Clear();
        records.ForEach(x => projects.Add(x));
    }
}

在Windows窗体应用程序中,我从AccesDataBase例程获取错误消息:

System.NotSupportedException:'AppLibrary.Models.ClientModel类型的成员ClientId不能用作参数值'

所以我认为这里的基本问题是我是否正确使用了ComboBox属性?我想念的是什么?

在此先感谢您的帮助。 Verônica。

解决方法

我发现自己的错误。

组合框的属性正确。

在我此处未共享的其他代码部分中,我将错误的参数传递给ClientDropDown.SelectedValue。

我试图通过代码在ClientDropDown中选择特定的客户端,但是我将SelectedIndex传递给SelectedValue。

我使用断点并能够找到错误,现在正在使用我在问题中共享的此代码,这是正确的。