内部联接 2 表如何在 vb.net 的 MS-ACCESS 数据库中使用 dapper 显示 datagridview 中的特定字段

how inner join 2 tables display specific fields in datagridview with dapper in MS-ACCESS database in vb.net

提问人:roy 提问时间:8/15/2023 更新时间:8/18/2023 访问量:62

问:

为什么 StocksoutDetail 表中的“codeproduct”没有出现在 datagridview 中,我的代码是否有问题,以及为什么在 datagridview 中出现另一个字段,即使我没有在 SQL 中选择该字段。请指导我

谢谢

Public Class Form1
    Private sosservice As New StocksoutService()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = sosservice.GetStockOut()
    End Sub
End Class
Public Class StocksoutService
    Public Function GetOledbConnectionString() As String
        Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\dapperdemo.accdb;Persist Security Info=False;"
    End Function
    Private ReadOnly _conn As OleDbConnection
    Private _connectionString As String = GetOledbConnectionString()
    Public Sub New()
        _conn = New OleDbConnection(_connectionString)
    End Sub
    Public Function GetStockOut() As IEnumerable(Of Stocksout)
        Dim sql = "SELECT Stocksout.Invno AS [Invno],StocksoutDetail.CodeProduct AS [CodeProduct] FROM Stocksout INNER JOIN StocksoutDetail ON Stocksout.Invno = StocksoutDetail.Invno"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of Stocksout)(sql).ToList()
        End Using
    End Function
End Class
Public Class Stocksout
    Public Property Invno() As String
    Public Property HeaderInvno() As Integer
    Public Property CreatedBy() As String
    Public Property Created() As DateTime
    Public Property ModifiedBy() As String
    Public Property Modified() As DateTime
    Public Property StocksoutDetail() As New List(Of StocksoutDetail)()
End Class
Public Class StocksoutDetail
    Public Property Id() As Integer
    Public Property No() As Integer
    Public Property Invno() As String
    Public Property CodeProduct() As String
    Public Property Barcode() As String
    Public Property Colorcode() As String
    Public Property Size() As String
    Public Property Qty() As Integer
End Class

result in datagridview

SQL vb.net MS-Access DataGridView dapper

评论

0赞 HardCode 8/16/2023
仅仅因为您查询了 Stocksout 对象的几个字段,数据绑定就不知道要隐藏或显示哪些字段。你必须自己做。当您绑定到一个对象时,它将显示该对象的所有字段,无论您是否使用数据库中的数据加载它们。我建议你“把它弄平”......创建一个 DTO 对象,该对象的属性仅包含要在 DGV 中显示的字段,并从数据库中加载该对象,并将 List(Of NewObject) 绑定到 DGV。NewObject 可以是 Stocksout 中的几个字段和 StocksoutDetail 中的几个字段,无论您需要什么。
0赞 Hursey 8/16/2023
据推测,Stocksout 和 StocksoutDetail 之间存在 1:M 关系(即stocksout 中一条记录,stocksoutdetail 中有许多记录),通常用 DVG 术语称为主/细节,使用标准网格控件可能无法很好地显示。你可能想读一读这个。或者,有许多第三方网格控件可以执行此操作。或者查看树视图
0赞 roy 8/16/2023
@HardCode,感谢您的回复。 为此请指导我I'd recommend you "flatten it out"... create a DTO object with properties of only the fields you want to display in the DGV
0赞 roy 8/16/2023
@Hursey,感谢您的回复,是的,您说得对,但仍然可以使用 DataGridView 显示而无需使用第三方
1赞 HardCode 8/17/2023
@pret我应该说的,创建一个 DTO(属性,无函数),其中包含您希望在 DGV 上看到的所有属性。然后,使用数据库中的数据加载从该类实例化的对象,并将该对象绑定到 DGV。class

答:

1赞 roy 8/18/2023 #1

根据@HardCode的指南和建议

  Private sosservice As New StocksoutService()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = sosservice.GetStockOut()
    End Sub

    Public Function GetStockOut() As IEnumerable(Of DTOStocksout)
        Dim sql = "SELECT Stocksout.Invno AS [Invno],StocksoutDetail.CodeProduct AS [CodeProduct] FROM Stocksout INNER JOIN StocksoutDetail ON Stocksout.Invno = StocksoutDetail.Invno"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of DTOStocksout)(sql).ToList()
        End Using
    End Function
Public Class DTOStocksout
    Public Property Invno() As String
    Public Property CodeProduct() As String

End Class