SQLite with Dapper C# - Windows Form:使用 ComboBox Lisl Properties 使用外键检索数据库数据

SQLite with Dapper C# - Windows Form: Using ComboBox Lisl Properties to retrieve database data with a foreign key

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

这就是我正在努力解决的问题。

我的数据库中有 2 个 table 已连接。 parent、tbClient。还有 child table, tbProject.

tbProject 有一个 ClientId 字段。

我使用 ComboBox 将数据从数据库 WireUpp 到我的表单。我有一个给客户的表格,还有一个给项目的表格,在这个表格中,我在 ComboBox 中选择了一个 CLient,并将其 ID 保存在我的 tbProjet 中。

这个想法很简单,但我很挣扎,因为我使用的是在 Windows (WPF) 中制作的示例,而我的应用程序是在 Windows 表单中。我注意到 ComboBox 的属性不一样,然后当我想打开特定客户的项目时,我无法访问正确的项目字段。

让我们展示一下它是如何在 WPF 应用程序和我的 WinForm 应用程序中完成的。我认为得到一些帮助会更清楚。

Project Form的代码如下:第一个是WPF app,第二个是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, 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 } 
         // --> 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: 'The member ClientId of type AppLibrary.Models.ClientModel cannot be used as a parameter value'

所以我认为这里的基本问题是我使用 ComboBOx 属性是否正确?我缺少什么?

提前感谢您提供的任何帮助。 维罗妮卡.

我发现了我的错误。

组合框的属性是正确的。

我在代码的其他部分向 ClientDropDown.SelectedValue 传递了错误的参数,我没有在这里分享。

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

我使用了断点并找到了错误,现在 正在使用我在问题中分享的这段代码,它是正确的。