2014-12-01 11 views
8

Ciao mi sono imbattuto in un problema durante la creazione del mio progetto scolastico.Recupero dei valori di classe in base a una chiave

Alcuni descrizione:

Listy - è un oggetto, ed il suo essere accluso da una query SQL e successivamente diventa un listbinding. Clienti - Clienti: id, nome, cognome

Listy sql discarica esempio

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

Il problema

mio punto di vista datagrid appare esattamente nello stesso modo come risultato mysql, quello che voglio get è una visualizzazione più amichevole, quindi se ho ottenuto byhwo 202 (è un ID cliente) voglio essere visualizzato nella vista datagrid Esempio Nome Esempio Cognome. Deve essere fatto con questo codice in qualche modo. Inoltre, una buona funzionalità potrebbe essere quella di eliminare e aggiornare in qualche modo un cliente di classe.

classe principale

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 

Classe I clienti

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 

Classe 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

Attualmente la tua domanda mescola un sacco di termini tecnici e non fornisce quasi nessuna informazione utile sul problema. Non cercare di spiegare il codice o l'approccio che hai provato. Cerca di spiegare il problema reale che stai cercando di risolvere, in inglese. Cerca di essere il più specifico possibile, usando 20 parole o giù di lì. È quindi possibile elaborare di più su di esso. – Neolisk

+0

@Neolisk Ok fammi abbreviare questo: clienti di classe (proprietà get/set), ho ottenuto una funzione per aggiungere il risultato sql ai clienti della classe (list.add nuovo cliente) quello che voglio è la possibilità di ottenere ad esempio il cliente (1) 1 è l'id e un esempio di ritorno è il nome e il cognome del cliente – Kavvson

+0

Quindi si desidera "selezionare * dai clienti DOVE customer_id = 1" e quindi l'elenco dei clienti per contenere esattamente un articolo? – Neolisk

risposta

5

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

In tal caso, il DB potrebbe essere mal progettato o strutturato, oppure la query SQL potrebbe essere sub ottimale. 50k non è un sacco di dati, e il precaricamento di tutti i dati e l'utilizzo di 3 classi per codificare manualmente lo stesso risultato non è probabile che sia molto più veloce, ma sarà più soggetto a errori.

Detto questo, la tua funzione display_single_name() potrebbe essere piuttosto semplice. Assumendo: lista

  • La mailing dati vengono caricati da qualche parte come un List(of MailItem)
  • L'applicazione ha qualcosa come il Customer classe e una lista (dei clienti)
  • Sappiamo che ByWho e ForWho sono FKS, ma cosa sono FK a non è ancora chiaro. Io parto dal presupposto clienti perché non ci sono altri attori menzionati

il codice esistente non ha dove memorizzare queste informazioni, in modo da pochi cambiamenti:

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 

Di seguito utilizza myMailItems che è un List(Of MailItem) per memorizzare le informazioni sugli articoli di posta elettronica inclusi i nomi risolti forniti, e CustList è un List(of Customer). Questo funzionalmente sarebbe fondamentalmente Core.init_customers() ma non è necessario che sia una classe speciale.

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

La ricerca cliente viene risolto tramite una funzione di supporto:

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

    Return cust 
End Function 

Nei dati di esempio, si definirebbe con 7, 3 e 202 (apparentemente) per ottenere il nome del cliente connessi. La nuova proprietà FullName formattata come preferisci, restituisce il nome per la memorizzazione nell'elenco.

La classe MailItem può eseguire la ricerca stessa nel costruttore, purché abbia un riferimento all'elenco Clienti. Ovviamente, potrebbe anche fare una ricerca SQL per ottenere il nome (i) per ogni attore.


Varianti per il look up:

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

rimodellare la funzione per ottenere il nome:

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) 

Nota:

Questa non è affatto una soluzione migliore n di una query SQL JOIN; ma sembra essere quello che stai cercando.

Affinché la ricerca a lavorare, è necessario portare tutti i clienti nel DB al PC client solo nel caso in cuipotrebbe essere utilizzato nella corrente dataset "listy". Quindi, il codice deve creare un oggetto per ciascuno e memorizzarlo. Ci vorrà tempo e memoria.

Un'alternativa sarebbe quella di sparare le ricerche SQL per ottenere Cust # 3 quando necessario e cache/riporlo in un elenco, in modo se si è visto ancora una volta è possibile riutilizzarlo e ha colpito solo il DB se non è nella lista. Ma di nuovo, stai semplicemente facendo in codice cosa può essere fatto con una query SQL JOIN appropriata.

+0

Chiarito la mia domanda dai un'occhiata. La tua risposta potrebbe essere in parte buona, potrei aver bisogno anche del display. – Kavvson

Problemi correlati