我正在研究這個搜索按鈕,我設法使它工作。唯一不利的一面是,當我嘗試搜索一個不同的項目時,它會繼續將我搜索的舊項目放回原處。可能是什麼問題呢?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玩,因爲我認爲這是我放在錯誤的東西。
有幾種事情是錯的,但你不需要通過像這樣的變量跳轉蘇格蘭數據。 'txtSurName.Text = rdr(「StudentLastName」)。ToString()'將工作花花公子。我不認爲有什麼東西會讓你的SQL搞砸了。 ''SELECT * FROM Student''抓住它們並且根本不搜索 - 它正在取代它上面的更好的查詢。也使用SQL參數和DBObjects DIspose – Plutonix