您好我在创建学校项目时遇到了一个问题。
一些描述:
Listy - 它的一个对象,它被一个sql查询追加,后来成为一个列表绑定。 客户 - 客户:身份证,姓名,姓氏
Listy sql dump示例
id+ number +letters+forwho+bywho+created +prority+type
7 900000170300000935295877 0 3 202 2013-11-27 16:37:55 0 1
问题
我的datagrid视图看起来与mysql结果完全相同,我想得到的是一个更友好的显示,所以如果我得到byhwo 202(它的客户ID)我想在datagrid视图中显示示例名称示例姓。必须以某种方式完成此代码。另外一个很好的功能是能够以某种方式删除和更新类客户。
类核心
Class Core
Dim gridDataList As New BindingList(Of Listy)
Dim cmd As New MySqlCommand
Dim da As New MySqlDataAdapter
Dim con As MySqlConnection = jokenconn()
Public list As New List(Of Customers)
Public Function jokenconn() As MySqlConnection
Return New MySqlConnection(.......)
End Function
Public Sub init_customers()
' Create a list of strings.
Dim sql As String
Dim myReader As MySqlDataReader
con.Open()
sql = "select * from customers"
'bind the connection and query
With cmd
.Connection = con
.CommandText = sql
End With
myReader = cmd.ExecuteReader()
While myReader.Read()
list.Add(New Customers(myReader.GetInt64(0), myReader.GetString(1), myReader.GetString(2)))
End While
con.Close()
End Sub
Public Function display_single_name()
Return 0
'Dim pinfo As propertyinfo = GetType(String).GetProperty("")
'here i want to return the name and surname of client based on a number/id
End Function
End Class
类客户
Class Customers
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal surname As String)
Me.ID = id
Me.Imie = name
Me.Nazwisko = surname
End Sub
#Region "Get/Set"
Public Property ID() As Integer
Get
Return Me._id
End Get
Set(ByVal value As Integer)
Me._id = value
End Set
End Property
Public Property Imie() As String
Get
Return Me._imie
End Get
Set(ByVal value As String)
Me._imie = value
End Set
End Property
Public Property Nazwisko() As String
Get
Return Me._nazwisko
End Get
Set(ByVal value As String)
Me._nazwisko = value
End Set
End Property
#End Region
Private _id As Integer
Private _imie As String
Private _nazwisko As String
End Class
班级列表
Class Listy
' Private _comments As String
' Private _firstName As String
' Private _secondName As String
Public Sub New(ByVal id As Integer, ByVal listnumb As String, ByVal list_count As Integer, ByVal by_who As Integer, ByVal for_who As Integer, ByVal created As Date, ByVal prority As Integer, ByVal type As Integer)
Me.ID = id
Me.Lista = listnumb
Me.Listów = list_count
Me.Wystawione_przez = by_who
Me.Wystawione_na = for_who
Me.Priorytet = prority
Me.Rodzaj_Listy = type
Me.Utworzono = created
End Sub
#Region "Get/Set"
Public Property ID() As Integer
Get
Return Me._id
End Get
Set(ByVal value As Integer)
Me._id = value
End Set
End Property
Public Property Lista() As String
Get
Return Me._list_Number
End Get
Set(ByVal value As String)
Me._list_Number = value
End Set
End Property
Public Property Listów() As Integer
Get
Return Me._Lst_Count
End Get
Set(ByVal value As Integer)
Me._Lst_Count = value
End Set
End Property
Public Property Wystawione_przez() As Integer
Get
Return Me._bywho
End Get
Set(ByVal value As Integer)
Me._bywho = value
End Set
End Property
Public Property Wystawione_na() As Integer
Get
Return Me._forwho
End Get
Set(ByVal value As Integer)
Me._forwho = value
End Set
End Property
Public Property Priorytet() As Integer
Get
Return Me._prority
End Get
Set(ByVal value As Integer)
Me._prority = value
End Set
End Property
Public Property Rodzaj_Listy() As Integer
Get
Return Me._type
End Get
Set(ByVal value As Integer)
Me._type = value
End Set
End Property
Public Property Utworzono() As Date
Get
Return Me._date
End Get
Set(ByVal value As Date)
Me._date = value
End Set
End Property
#End Region
Private _id As Integer
Private _Lst_Count As Integer
Private _bywho As Integer
Private _forwho As Integer
Private _prority As Integer
Private _type As Integer
Private _date As Date
Private _list_Number As String
End Class
答案 0 :(得分:5)
a Listy query has for example 50k rows so it can become slow
在这种情况下,您的数据库可能设计或结构不合理,或者SQL查询可能是次优的。 50k不是很多数据,并且预加载所有数据并使用3个类来手动编码相同的结果可能不会快得多,但会更容易出错。
那就是说,你的display_single_name()
功能可能非常简单。假设:
List(of MailItem)
Customer
类和List(客户)现有代码无法存储此信息,因此需要进行一些更改:
Class Customer
...
Public ReadOnly Property FullName As String
Get
Return String.Format("{0} {1}", Name, LastName)
' or
'Return String.Format("{0}, {1}", LastName, Name)
End Get
End Property
...
Public Class MailItem ' AKA "listy"
Public Property CustomerID As Integer
Public Property CustomerName As String
Public Property ListNumber As String
Public Property ListCount As Integer
Public Property ByWhomID As Integer
Public Property ByWhomName As String
Public Property ForWhomID As Integer
Public Property ForWhomName As String
Public Property Priority As Integer
...etc
以下使用myMailItems
List(Of MailItem)
来存储邮件项目信息,包括已提供给它的解析名称,CustList
是List(of Customer)
。这在功能上基本上是Core.init_customers()
,但实际上并不需要它成为一个特殊的类。
' loading the MailItems ("listy") into a
' list(of MainItem) ("gridDataList"??? it is never used in the OP code
' rdr is a SQLReader
Dim mi As New MailItem(Convert.ToInt32(rdr.Item("ID"))
... all the other fields)
Dim tmpCust As Customer
' load the names of the actors from the ID:
tmpCust = GetCustomerByID(mi.CustomerID)
If tmpCust IsNot Nothing Then
mi.CustomerName = tmpCust.FullName
Else
mi.CustomerName = "Unknown!"
End If
tmpCust = GetCustomerByID(mi.ForWhomID)
If tmpCust IsNot Nothing Then
mi.ForWhomName = tmpCust.FullName
Else
mi.ForWhomName = "Unknown!"
End If
tmpCust = GetCustomerByID(mi.ByWhomID)
If tmpCust IsNot Nothing Then
mi.ByWhomName = tmpCust.FullName
Else
mi.ByWhomName = "Unknown!"
End If
' the actor names are resolved, add to the list:
myMailItems.Add(mi)
通过辅助函数解析客户查询:
Private Function GetCustomerByID(id As Integer) As Customer
Dim cust As Customer = CustList.Find(Function(x) x.ID = id)
Return cust
End Function
在示例数据中,您将使用7,3和202(显然)调用它来获取相关的客户名称。无论您喜欢格式化的新FullName
属性,都会返回名称以便存储在列表中。
MailItem
类可以在构造函数中执行查找,前提是它具有对Customer列表的引用。当然,它也可以进行SQL查找以获取每个actor的名称。
查找变体:
' a simple loop:
For Each Cust As Customer In CustList
If Cust.Id = id Then Return Cust
End If
Return Nothing
重新绘制函数以获取名称:
Function CustmerNameFromID(id as Integer) As String
' or use the loop variant here
Dim cust As Customer = CustList.Find(Function(x) x.ID = id)
' test for Nothing here rather than in data load proc
If cust IsNot Nothing Then
Return cust.FullName
Else
Return ""
End If
End Function
' use:
mi.CustomerName = CustmerNameFromID(mi.ID)
注意:
这绝不是一个比SQL JOIN查询更好的解决方案; 但它似乎确实是您正在寻找的。
为了使查找起作用,您必须将所有数据库中的客户带到客户端PC 以防万一他们可能在当前的“listy”数据集中使用。然后,代码必须为每个创建一个对象并存储它。这需要时间和记忆。
另一种方法是在需要时触发SQL查找以获取Cust#3并将其缓存/存储在List中,因此如果再次看到它,您可以重复使用它并且只能访问数据库如果它不在列表中。但同样,您只是在代码中使用正确的SQL JOIN查询来完成。