提问人:roy 提问时间:8/15/2023 更新时间:8/18/2023 访问量:62
内部联接 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
问:
为什么 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
答:
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
评论
I'd recommend you "flatten it out"... create a DTO object with properties of only the fields you want to display in the DGV
class