如何在 VB.NET 中将数据从 Dapper ExecuteReader() 检索到具有 MS-ACCESS 数据库的文本框中

How to retrieve data from Dapper ExecuteReader() into a textbox with MS-ACCESS database in VB.NET

提问人:roy 提问时间:8/21/2023 更新时间:8/21/2023 访问量:84

问:

我想在 Invono 的 TextBox1 和 Created 的 TextBox2 的 Form2 中检索数据。 实际上,我已经创建了一个函数方法,但我不知道如何使用它在文本框中显示。请指导。GetItemTransfersMaster

谢谢

Form1 中的代码

Public Class Form1
    Dim itrservice As New ItemtransfersService()
    Private Invno As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = itrservice.GetLoadItemTransfersMaster()
        DataGridView1.ReadOnly = True
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.ColumnIndex = 0 Then
            If DataGridView1.SelectedRows.Count > 0 Then ' make sure user select at least 1 row
                Dim Invno As String = DataGridView1.SelectedRows(0).Cells(0).Value & String.Empty
                Using frm = New Form2(Invno)
                    If frm.ShowDialog() = DialogResult.OK Then

                    End If
                End Using
            End If
        End If
    End Sub
End Class
Public Class ItemtransfersService
    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 GetLoadItemTransfersMaster() As IEnumerable(Of ItemTransfers)
        Dim sql = "SELECT * FROM ItemTransfers"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of ItemTransfers)(sql).ToList()
        End Using
    End Function
    Public Function GetItemTransfersDetail(ByVal Invno As String) As IEnumerable(Of ItemTransfersDetail)
        Dim sql = $"SELECT * FROM ItemTransfersDetail WHERE Invno = '{Invno}'"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of ItemTransfersDetail)(sql).ToList()
        End Using
    End Function
    Public Function GetItemTransfersMaster(ByVal Invno As String) As IEnumerable(Of ItemTransfers)
        Dim sql = $"SELECT * FROM ItemTransfers WHERE Invno = '{Invno}'"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of ItemTransfers)(sql).ToList()
        End Using
    End Function
End Class
Public Class ItemTransfers
    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 ItemTransfersDetail() As New List(Of ItemTransfersDetail)()
End Class
Public Class ItemTransfersDetail
    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

Form2 中的代码

Public Class Form2
    Inherits Form
    Dim itrservice As New ItemtransfersService()
    Sub New()
        InitializeComponent()
    End Sub
    Public Sub New(Invno As String)
        Me.New
        DataGridView1.DataSource = itrservice.GetItemTransfersDetail(Invno)
        DataGridView1.Columns(0).Visible = False
    End Sub
End Class

在 Form1 中查看

view in form1

在 Form2 中查看

view in form2

SQL vb.net LINQ Dapper

评论

1赞 Panagiotis Kanavos 8/21/2023
什么文本框?无论如何,代码误用了 Dapper。Dapper 的全部意义在于使执行参数化查询更容易。' $“SELECT * FROM ItemTransfersDetail WHERE Invno = '{Invno}'”' 是一个错误。请改用,并使用 执行它。这将生成一个参数化查询,并将 的值作为参数值传递Where InvNo=@InvNo"_conn.Query(Of ItemTransfersDetail)(sql,new {InvNo=InvNo})InvNo@InvNo
1赞 Panagiotis Kanavos 8/21/2023
实际问题是如何将数据传递到子窗体并将其显示在文本框中吗?您需要将该数据传递到其他窗体。代替或读取单元格,您可以使用 传递用于生成网格行的实际对象。.SelectedRows(0).DataBoundItem
0赞 roy 8/21/2023
@PanagiotisKanavos,感谢您的回复。 textbox 我的意思是 form2 中的文本框有 2 个。 $“SELECT * FROM ItemTransfersDetail WHERE Invno = '{Invno}'”' 是一个错误。请改用 Where InvNo=@InvNo“,并使用 _conn 执行它。Query(Of ItemTransfersDetail)(sql,new {InvNo=InvNo})。这将生成一个参数化查询,并将 InvNo 的值作为@InvNo参数值传递。 感谢您的建议和指导What textbox?The whole point of Dapper is to make executing parameterized queries easier.
0赞 roy 8/21/2023
@PanagiotisKanavos,请指导我使用它,如果我通过 dapper 使用该功能,可能会实现什么SelectedRows(0).DataBoundItem.
1赞 roy 8/21/2023
@dr.null,你说得对。谢谢你给我最好的指导。我也更新了答案The item object contains it item.Invno if I'm not mistaken.

答:

1赞 roy 8/21/2023 #1

非常感谢 @dr.null 的指导

表单 1 中的代码

Public Class Form1
    Dim itrservice As New ItemtransfersService()
    Private Invno As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = itrservice.GetLoadItemTransfersMaster()
        DataGridView1.ReadOnly = True
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.ColumnIndex = 0 Then
            If DataGridView1.SelectedRows.Count > 0 Then ' make sure user select at least 1 row
                Dim Invno As String = DataGridView1.SelectedRows(0).Cells(0).Value.tostring & String.Empty
 Dim item = DirectCast(DataGridView1.SelectedRows(0).DataBoundItem, ItemTransfers)
                Using frm = New Form2(item)
                    If frm.ShowDialog() = DialogResult.OK Then

                    End If
                End Using
            End If
        End If
    End Sub
End Class
Public Class ItemtransfersService
    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 GetLoadItemTransfersMaster() As IEnumerable(Of ItemTransfers)
        Dim sql = "SELECT * FROM ItemTransfers"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of ItemTransfers)(sql).ToList()
        End Using
    End Function
    Public Function GetItemTransfersDetail(ByVal Invno As String) As IEnumerable(Of ItemTransfersDetail)
        Dim sql = $"SELECT * FROM ItemTransfersDetail WHERE Invno = '{Invno}'"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of ItemTransfersDetail)(sql).ToList()
        End Using
    End Function
    Public Function GetItemTransfersMaster(ByVal Invno As String) As IEnumerable(Of ItemTransfers)
        Dim sql = $"SELECT * FROM ItemTransfers WHERE Invno = '{Invno}'"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of ItemTransfers)(sql).ToList()
        End Using
    End Function
End Class
Public Class ItemTransfers
    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 ItemTransfersDetail() As New List(Of ItemTransfersDetail)()
End Class
Public Class ItemTransfersDetail
    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

表单 2 中的代码

Public Class Form2
    Inherits Form
Dim itrservice As New ItemtransfersService()
    Sub New()
        InitializeComponent()
    End Sub
 Public Sub New(item As ItemTransfers)
        Me.New
        DataGridView1.DataSource = itrservice.GetItemTransfersDetail(Invno)
        DataGridView1.Columns(0).Visible = False
        TextBox1.Text = item.Invno
        TextBox2.Text = item.Created
 End Sub
End Class