2012-09-30 20 views
5

Buona giornata a tutti! Vorrei chiedere aiuto per quanto riguarda il mio codice qui. La preoccupazione principale è quella di cercare dal database MySQL la parola chiave fornita da una casella di testo. Ecco la mia GUI per riferimento.VB.NET - Funzione di ricerca con casella di testo e casella combinata

The GUI of my program

Quando digito la mia chiave di ricerca nella casella di testo e selezionato una colonna sulla casella combinata, la query di ricerca avrà la sua uscita sul ListView. Ho provato numerose combinazioni per ottenere un risultato, ma senza risultato.

Ecco il mio codice per aiutarmi.

Private Sub Search() 

    lviClientList.Items.Clear() 
    Dim strSqlSearch As String = "SELECT code, Company, StAdd, City, ContactPerson, Phone, Mobile, Email, Remarks FROM tblclients WHERE '@Column' LIKE '%" & txtSearchCriteria.Text & "%'" 

    Dim item As New ListViewItem() 

    If cboColumns.SelectedIndex = 0 Then 
     column = "code" 
    ElseIf cboColumns.SelectedIndex = 1 Then 
     column = "Company" 
    ElseIf cboColumns.SelectedIndex = 2 Then 
     column = "StAdd" 
    ElseIf cboColumns.SelectedIndex = 3 Then 
     column = "City" 
    ElseIf cboColumns.SelectedIndex = 4 Then 
     column = "ContactPerson" 
    ElseIf cboColumns.SelectedIndex = 5 Then 
     column = "Phone" 
    ElseIf cboColumns.SelectedIndex = 6 Then 
     column = "Mobile" 
    ElseIf cboColumns.SelectedIndex = 7 Then 
     column = "Email" 
    ElseIf cboColumns.SelectedIndex = 8 Then 
     column = "Remarks" 
    End If 

    Dim mysqlCommand As New MySqlCommand(strSqlSearch, mysqlConnection) 
    mysqlCommand.Parameters.AddWithValue("@Column", column) 

    Try 
     mysqlConnection.Open() 
     mysqlReader = mysqlCommand.ExecuteReader() 

     Do While mysqlReader.Read() 

      item = lviClientList.Items.Add(mysqlReader("code").ToString) 
      item.SubItems.Add(mysqlReader("Company").ToString) 
      item.SubItems.Add(mysqlReader("StAdd").ToString) 
      item.SubItems.Add(mysqlReader("City").ToString) 
      item.SubItems.Add(mysqlReader("ContactPerson").ToString) 
      item.SubItems.Add(mysqlReader("Phone").ToString) 
      item.SubItems.Add(mysqlReader("Mobile").ToString) 
      item.SubItems.Add(mysqlReader("Email").ToString) 
      item.SubItems.Add(mysqlReader("Remarks").ToString) 

     Loop 

    Catch ex As Exception 

     MsgBox("No results found.", MsgBoxStyle.OkOnly, "Project Analysis System") 

    Finally 

     mysqlReader.Close() 
     mysqlConnection.Close() 

    End Try 

End Sub 

risposta

3

Non è chiaro perché il codice non funzioni correttamente. Prova a cambiare il codice nel tuo Catch clausola

MsgBox("No results found.", MsgBoxStyle.OkOnly, "Project Analysis System") 

in

Msgbox(ex.Message.ToString(), MsgBoxStyle.OkOnly, "Project Analysis System") 

modo si sa che cosa l'errore esatto è.

È possibile concatenare il valore per ColumnName poiché è impostato in modo statico nel codice. Ma il valore su WHERE deve essere parametrizzato poiché è quello inserito dall'utente.

provare questo codice modificato,

Private Sub Search() 

    lviClientList.Items.Clear() 
    Dim item As New ListViewItem() 
    Dim _isFound As Boolean = False 

    Dim colName() As String = {"code", "Company", "StAdd", "City", "ContactPerson", "Phone", "Mobile", "Email", "Remarks"} 

    Dim strSqlSearch As String = "SELECT code, Company, StAdd, City, " & _ 
            "ContactPerson, Phone, Mobile, Email, Remarks " & _ 
            "FROM tblclients " & _ 
            "WHERE " & colName(cboColumns.SelectedIndex) & " LIKE CONCAT('%', @valueName, '%')" 

    Using myConn As New MySqlConnection("connectionStringHere") 
     Using myComm As New MySqlCommand() 
      With myComm 
       .Connection = myConn 
       .CommandType = CommandType.Text 
       .CommandText = strSqlSearch 
       .Parameters.AddWithValue("@valueName", txtSearchCriteria.Text); 
      End With 
      Try 
       myConn.Open() 
       Dim myReader As MySqlDataReader = myComm.ExecuteReader() 

       While myReader.Read() 
        _isFound = True 
        item = lviClientList.Items.Add(myReader("code").ToString) 
        item.SubItems.Add(myReader("Company").ToString) 
        item.SubItems.Add(myReader("StAdd").ToString) 
        item.SubItems.Add(myReader("City").ToString) 
        item.SubItems.Add(myReader("ContactPerson").ToString) 
        item.SubItems.Add(myReader("Phone").ToString) 
        item.SubItems.Add(myReader("Mobile").ToString) 
        item.SubItems.Add(myReader("Email").ToString) 
        item.SubItems.Add(myReader("Remarks").ToString) 
       End While 

       If Not _isFound Then 
        MsgBox("No results found.", MsgBoxStyle.OkOnly, "Project Analysis System") 
       End If 

      Catch ex As MySqlException 
       Msgbox(ex.Message.ToString(), MsgBoxStyle.OkOnly, "Project Analysis System") 
      End Try 
     End Using 
    End Using 

End Sub 
+0

Cercherò codice. :) –

+0

in caso di errore, gentilmente postare qui grazie: D –

+1

Ehi, ha funzionato! Grazie mille signore. –

Problemi correlati