2015-09-08 275 views
0

我正在研究這個搜索按鈕,我設法使它工作。唯一不利的一面是,當我嘗試搜索一個不同的項目時,它會繼續將我搜索的舊項目放回原處。可能是什麼問題呢?VB.net搜索按鈕

下面的代碼:

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click 

    com.Connection = con 
    com.CommandText = ("Select Student_ID from Student where Student_ID = '" & txtSearch.Text & "'") 
    com.CommandText = "SELECT * FROM Student" 

    OpenDB() 
    Dim SurName As String = "" 
    Dim MiddleName As String = "" 
    Dim FirstName As String = "" 
    Dim SAddress As String = "" 
    Dim Birthday As String = "" 
    Dim Age As String = "" 
    Dim Birthplace As String = "" 
    Dim SContactNumber As String = "" 
    Dim GradeYear As String = "" 
    Dim SchoolYear As String = "" 
    Dim ParentGaurdian As String = "" 
    Dim PGContact As String = "" 
    Dim PGAddress As String = "" 
    rdr = com.ExecuteReader 

    If rdr.Read = True Then 
     SurName = rdr("StudentLastName").ToString 
     MiddleName = rdr("StudentMiddleName").ToString 
     FirstName = rdr("StudentFirstName").ToString 
     SAddress = rdr("StudentAddress").ToString 
     Birthday = rdr("Birthday").ToString 
     Age = rdr("Age").ToString 
     Birthplace = rdr("BirthPlace").ToString 
     SContactNumber = rdr("StudentContactNumber").ToString 
     GradeYear = rdr("GradeYearLevel").ToString 
     SchoolYear = rdr("SchoolYear").ToString 
     ParentGaurdian = rdr("ParentName").ToString 
     PGContact = rdr("ParentContactNumber").ToString 
     PGAddress = rdr("ParentAddress").ToString 
    End If 

    con.Close() 
    com.Dispose() 
    rdr.Close() 

    txtSurName.Text = SurName 
    txtMiddleName.Text = MiddleName 
    txtFirstName.Text = FirstName 
    txtSAddress.Text = SAddress 
    txtBirthday.Text = Birthday 
    txtAge.Text = Age 
    txtBirthplace.Text = Birthplace 
    txtSContactNumber.Text = SContactNumber 
    txtGradeYear.Text = GradeYear 
    txtSchoolYear.Text = SchoolYear 
    txtParentGaurdian.Text = ParentGaurdian 
    txtPGContact.Text = PGContact 
    txtPGAddress.Text = PGAddress 
End Sub 

我試圖與周圍rdr.close玩,因爲我認爲這是我放在錯誤的東西。

+0

有幾種事情是錯的,但你不需要通過像這樣的變量跳轉蘇格蘭數據。 'txtSurName.Text = rdr(「StudentLastName」)。ToString()'將工作花花公子。我不認爲有什麼東西會讓你的SQL搞砸了。 ''SELECT * FROM Student''抓住它們並且根本不搜索 - 它正在取代它上面的更好的查詢。也使用SQL參數和DBObjects DIspose – Plutonix

回答

0

看起來像您的SQL查詢應該是更多這樣的:

'com.CommandText = ("Select Student_ID from Student where Student_ID = '" & txtSearch.Text & "'") 
'com.CommandText = "SELECT * FROM Student" 
com.CommandText = "SELECT * FROM Student WHERE Student_ID = '" & txtSearch.Text & "'" 
+0

不是我的DV,但SQL不應該使用串聯。 – Plutonix

+0

我同意不連接SQL,但這不是關於最佳實踐的問題。我很肯定我的答案解決了這個問題,並通過對海報原始代碼的最小改動來解決問題。 – Pasilda

+0

SO答案適用於年齡 - 將來會有很多人來看 - 答案是「你的SQL應該是這樣的:...」然後連接查詢給出了不好的建議。 – Plutonix

0

刪除下面兩行..

com.CommandText = ("Select Student_ID from Student where Student_ID = '" & txtSearch.Text & "'") 
com.CommandText = "SELECT * FROM Student" 

,而是使用以下..

com.CommandText = ("Select Student_ID from Student where Student_ID = @searchText") 
com.Parameters.AddWithValue("@searchText", txtSearch.Text)