2014-12-01 62 views
8

您好,我在創建我的學校項目時遇到了問題。基於密鑰檢索班級價值

一些描述:

Listy - 它的一個對象,並且其被一個SQL查詢所附和後來成爲listbinding。 客戶 - 客戶端:ID,姓名,姓

Listy sql中傾倒例如

id+ number     +letters+forwho+bywho+created   +prority+type 
7 900000170300000935295877 0  3  202 2013-11-27 16:37:55 0  1 

問題

我的數據網格視圖看起來完全相同的方式作爲MySQL的結果,我想什麼get是一個更友好的顯示,所以如果我得到了202(它的一個客戶ID)我想在數據網格視圖中顯示示例名稱示例姓氏。它必須以某種方式使用此代碼完成。另外一個很好的功能是能夠以某種方式刪除和更新Class客戶。

級核心

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 

類Listy

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 
+1

您的問題目前混入了許多技術性的熱門詞彙,並且幾乎沒有提供有關該問題的有用信息。不要試圖解釋代碼或嘗試的方法。嘗試用英語解釋你試圖解決的實際問題。嘗試儘可能具體,使用20個字左右。然後你可以詳細闡述它。 – Neolisk 2014-12-06 19:36:31

+0

@Neilisk好吧,讓我縮短這個:Class Customers(get/set property),我有一個函數將sql結果附加到類的客戶(list.add新客戶)我想要的是能夠獲得例如客戶1)1是id並且示例返回是客戶的姓名 – Kavvson 2014-12-06 22:07:39

+0

因此,您想要「從客戶的select * customer_id = 1」,然後您的客戶列表中只包含一個項目? – Neolisk 2014-12-06 22:40:17

回答

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(客戶的)
  • 我們知道,ByWho和ForWho是FKS,但什麼他們是FKs 目前尚不清楚。我假設客戶因爲沒有其他演員提到

現有的代碼有沒有地方來存儲這些信息,所以一些改動:

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)存儲包含解析名稱的郵件項目信息以及CustListList(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查找以獲取每個演員的姓名。


變數的擡頭:

' a simple loop: 
For Each Cust As Customer In CustList 
    If Cust.Id = id Then Return Cust 
End If 
Return Nothing   

Recraft的函數來獲取名稱:

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) 

注:

這絕非更好的解決方案n比SQL JOIN查詢;但它似乎是你在找什麼。

爲了查找工作,你必須把所有客戶在DB到客戶端PC 以防萬一他們可能在目前「listy」數據集中使用。然後,代碼必須爲每個對象創建一個對象並存儲它。這將需要時間和記憶。

需要和它的緩存/存儲在一個列表時,另一種方法是,以火過SQL查詢來獲得卡斯特#3,所以如果它再次看到你可以重複使用它,只打了DB如果不是在列表中。但是,再次,您只是在代碼中執行正確的SQL JOIN查詢。

+0

澄清我的問題看看它。您的答案可能部分不錯,我可能也需要顯示器。 – Kavvson 2014-12-07 08:56:29