提问人:roy 提问时间:8/21/2023 更新时间:8/21/2023 访问量:84
如何在 VB.NET 中将数据从 Dapper ExecuteReader() 检索到具有 MS-ACCESS 数据库的文本框中
How to retrieve data from Dapper ExecuteReader() into a textbox with MS-ACCESS database in VB.NET
问:
我想在 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 中查看
在 Form2 中查看
答:
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
评论
Where InvNo=@InvNo"
_conn.Query(Of ItemTransfersDetail)(sql,new {InvNo=InvNo})
InvNo
@InvNo
.SelectedRows(0).DataBoundItem
What textbox?
The whole point of Dapper is to make executing parameterized queries easier.
SelectedRows(0).DataBoundItem.
The item object contains it item.Invno if I'm not mistaken.