2014-12-01 19 views
8

Witam Natknąłem się na problem podczas tworzenia mojego projektu szkolnego.Pobieranie wartości klasy w oparciu o klucz

Niektóre opis

Listy - jego przedmiotem i jego dołączane przez zapytań SQL, a następnie staje się listbinding. Klienci - Klienci: id, imię, nazwisko

Listy sql zrzucić przykład

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

Problem

Mój pogląd DataGrid wygląda dokładnie tak samo, jak w wyniku mysql, co chcę get jest bardziej przyjaznym wyświetlaczem, więc jeśli dostałem byhwo 202 (jest to identyfikator klienta) chcę być wyświetlony w widoku datagrid Przykład Nazwa Przykład Nazwisko. W jakiś sposób należy to zrobić za pomocą tego kodu. Również fajną funkcją byłoby mieć możliwość usuwania i aktualizowania Klasy.

klasy bazowej

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 

Klienci 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 

Klasa 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

Twoje pytanie zawiera obecnie wiele technicznych słów buzz i prawie nie dostarcza użytecznych informacji o problemie. Nie próbuj wyjaśniać kodu lub podejścia, którego próbowałeś. Spróbuj wyjaśnić rzeczywisty problem, który próbujesz rozwiązać, w języku angielskim. Staraj się być jak najbardziej konkretny, używając 20 słów. Następnie możesz rozwinąć więcej na ten temat. – Neolisk

+0

@Neolisk Ok pozwól mi skrócić to: Klasy Klienci (uzyskać/ustawić właściwość), mam funkcję dołączania wyniku sql do klientów klasy (nowy klient list.add), czego chcę, to możliwość uzyskania na przykład klienta (1) 1 jest identyfikatorem, a przykładowym zwrotem jest imię i nazwisko klienta – Kavvson

+0

. Chcesz "wybrać * od klientów WHERE customer_id = 1", a następnie listę klientów zawierającą dokładnie jeden produkt? – Neolisk

Odpowiedz

5

a Listy query has for example 50k rows so it can become slow

W takim przypadku Twoja baza danych może być źle zaprojektowana lub zbudowana, lub zapytanie SQL może być nieoptymalne. 50k nie zawiera wielu danych, a wstępne załadowanie wszystkich danych i użycie 3 klas do ręcznego zakodowania tego samego wyniku nie będzie prawdopodobnie szybsze, ale będzie bardziej podatne na błędy.

To powiedziawszy, twoja funkcja display_single_name() może być całkiem prosta. Zakładając:

  • mailingu lista dane są ładowane gdzieś jak List(of MailItem)
  • Aplikacja ma coś takiego jak Customer klasy i listy (od klienta)
  • Wiemy, że ByWho i ForWho są FKS, ale co są to FKs do nadal nie jest jasne. Jestem zakładając Klientowi ponieważ nie istnieją żadne inne podmioty wymienione

istniejącego kodu nie ma gdzie przechowywać te informacje, więc kilka zmian:

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 

następujących zastosowań myMailItems który jest List(Of MailItem) do przechowywania informacje o elemencie pocztowym, w tym nazwach rozstrzygniętych, które zostały mu podane, a CustList to List(of Customer). Funkcjonalnie byłoby to w zasadzie zgodne z Core.init_customers(), ale nie ma potrzeby, aby była to specjalna klasa.

' 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) 

na wyszukanie klient został rozwiązany za pomocą funkcji pomocniczej:

Private Function GetCustomerByID(id As Integer) As Customer 
    Dim cust As Customer = CustList.Find(Function(x) x.ID = id) 

    Return cust 
End Function 

W przykładowych danych, można by nazwać z 7, 3 i 202 (podobno), aby uzyskać związane nazwę klienta. Nowa, sformatowana właściwość FullName zwraca nazwę do zapisania na liście.

Klasa MailItem może wykonać wyszukiwanie w konstruktorze, o ile ma odniesienie do listy klientów. Oczywiście, może również wykonać wygląd SQL, aby uzyskać nazwy dla każdego aktora.


Warianty dla patrzeć:

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

Recraft funkcji uzyskać nazwę:

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) 

Uwaga:

To jest w żaden sposób lepsze solutio n niż zapytanie SQL JOIN;, ale wygląda na to, czego szukasz.

Aby odnośnika do pracy, trzeba przynieść wszystkie klientów w DB do komputera klienckiego wszelki wypadek one może być stosowany w obecnym zbiorze „Listy”. Następnie kod musi utworzyć obiekt dla każdego i zapisać go. To zajmie trochę czasu i pamięci.

Alternatywą może być odpalenie odnośników SQL, aby uzyskać Cust # 3 w razie potrzeby i zapisanie w pamięci podręcznej/przechowywanie na liście, dzięki czemu można go ponownie użyć i uderzyć tylko w DB, jeśli nie jest na liście. Ale znowu robisz w kodzie, co można zrobić przy pomocy odpowiedniego zapytania SQL JOIN.

+0

Wyjaśniłem moje pytanie, spójrz na to. Twoja odpowiedź może być częściowo dobra, bo może potrzebować również wyświetlacza. – Kavvson

Powiązane problemy