So I want to without changing the function of the existing method because I will change all the function methods in the service whether it is a solution so that I can create a bindinglist by converting from an existing dapper service method. Please Guide me
所以我想不更改现有方法的功能,因为我将更改服务中的所有函数方法,无论它是一个解决方案,以便我可以通过转换现有的Dapper服务方法来创建绑定列表。请给我指路
Thanks
谢谢
Dim pservice As New personService
Private list As BindingList(Of persondetail) = Nothing
Private bindingSource As BindingSource = Nothing
Public Sub New(invono As personmaster)
Me.New
DataGridView1.DataSource =pservice.GetPersondetailbyinvono(invono.Invono))
End Sub
Public Function GetPersondetailbyinvono(ByVal Invno As String) As IEnumerable(Of persondetail)
Dim sql = $"SELECT * FROM PersonDetail WHERE Invono = '{Invno}'"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of persondetail)(sql).ToList()
End Using
End Function
Option 1
选项1
Private bindingSource As BindingSource = Nothing
Public Sub New(invono As personmaster)
Me.New
bindingSource = New BindingSource With {.DataSource = New BindingList(Of persondetail)(CType(pservice.GetPersondetailbyinvono(invono.Invono), IList(Of persondetail)))}
DataGridView1.DataSource = bindingSource
End Sub
Public Function GetPersondetailbyinvono(ByVal Invno As String) As IEnumerable(Of persondetail)
Dim sql = $"SELECT * FROM PersonDetail WHERE Invono = '{Invno}'"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of persondetail)(sql).ToList()
End Using
End Function
Option 2
备选案文2
Private bindingSource As BindingSource = Nothing
Public Sub New(invono As personmaster)
Me.New
DataGridView1.DataSource = New BindingList(Of persondetail)(CType(pservice.GetPersondetailbyinvono(invono.Invono), IList(Of persondetail)))
End Sub
Public Function GetPersondetailbyinvono(ByVal Invno As String) As IEnumerable(Of persondetail)
Dim sql = $"SELECT * FROM PersonDetail WHERE Invono = '{Invno}'"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of persondetail)(sql).ToList()
End Using
End Function
更多回答
Dapper is a data access technology while a BindingList
exists for the purposes of the UI. You should have your presentation layer create the BindingList
. One of the constructors takes an IList(Of T)
and wraps it, so you can pass in your existing list returned by Dapper. You're returning it as an IEnumerable(Of T)
though, so you'd have to change that or else cast. You could also call ToList
. That is technically safer but a bit wasteful if you know that it's already been called.
Dapper是一种数据访问技术,而BindingList是为了实现UI而存在的。您应该让表示层创建BindingList。其中一个构造函数接受IList(Of T)并包装它,这样您就可以传入Dapper返回的现有列表。不过,您会将其作为IEumable(Of T)返回,因此您必须更改它,否则将进行强制转换。您也可以调用ToList。这在技术上更安全,但如果您知道它已经被调用,就会有点浪费。
@jmcilhinney , Thank you for your response, please guide me. Meaning the best way I have to change directly to the binding list in the service method of the function.
@jmcilhinney,感谢您的回复,请指导我。这意味着我必须直接更改到函数的服务方法中的绑定列表的最佳方式。
I have guided you. Now it's time for you to try what I said and then update your question if you encounter further issues.
我已经指引了你。现在是你尝试我所说的,然后如果你遇到进一步的问题更新你的问题的时候了。
@jmcilhinney , sorry to disturb your time, I have managed to create a binding list with dapper, I have updated in the post please guide from you, because I made 2 options successful but which one I better use among the 2 options.
@jmcilhinney,抱歉打扰您的时间,我已经用Dapper创建了一个绑定列表,我已经在帖子中更新了请您指导,因为我成功地完成了两个选项,但我更好地使用了这两个选项中的哪一个。
我是一名优秀的程序员,十分优秀!