我在vb中有一个小型作业,可以搜索学生记录
搜索文本框用于记录和选择组合框中的列,
Private Sub search()
'search method for Search Record'
Try
If cmbKeywords.Text = "ID" Then
Dim dbQuery As String = "Select * from tblstudents where id =" & txtSearch.Text
ElseIf cmbKeywords.Text = "Name" Then
Dim dbQuery As String = "Select * from tblstudents where id =" & txtSearch.Text
ElseIf cmbKeywords.Text = "Address" Then
Dim dbQuery As String = "Select * from tblstudents where id =" & txtSearch.Text
ElseIf cmbKeywords.Text = "Contact" Then
Dim dbQuery As String = "Select * from tblstudents where id =" & txtSearch.Text
ElseIf cmbKeywords.Text = "Civil Status" Then
Dim dbQuery As String = "Select * from tblstudents where id =" & txtSearch.Text
End If
Dim dbConnection As New MySqlConnection(dbConString)
Dim dbCmd As New MySqlCommand(dbQuery, dbConnection)
Dim dbReader As MySqlDataReader
dbConnection.Open()
dbReader = dbCmd.ExecuteReader()
dgvRecords.ReadOnly = True
dgvRecords.ColumnCount = 6
dgvRecords.Columns(0).Name = "Stud ID"
dgvRecords.Columns(1).Name = "Name"
dgvRecords.Columns(2).Name = "Address"
dgvRecords.Columns(3).Name = "Gender"
dgvRecords.Columns(4).Name = "Status"
dgvRecords.Columns(5).Name = "Contact No"
While dbReader.Read()
dgvRecords.Rows.Add(dbReader("id"), dbReader("lastname") & ", " & dbReader("firstname") & " " & dbReader("middlename"), dbReader("address"), dbReader("gender"), dbReader("civil_status"), dbReader("contact"))
End While
dbReader.Close()
dbConnection.Close()
Catch ex As Exception
MsgBox("ERROR: " & ErrorToString(), MsgBoxStyle.Critical)
End Try
End Sub
我在这一行有错误。
Dim dbCmd As New MySqlCommand(dbQuery, dbConnection)
它说dbQuery没有定义。 有人可以帮助我吗? 对不起新手在vb这里。
答案 0 :(得分:0)
这是范围问题。如果您在dbQuery
或If
块中声明ElseIf
,则它仅存在于该块内。所有这些声明实际上都创建了单独的变量,这些变量在End If
语句中不再存在。
解决方案是仅在dbQuery
语句之前声明If
变量一次,然后在这些多个位置分配它。因为变量是在更宽的范围内声明的,所以可以在If Else
行之后访问它。
答案 1 :(得分:0)
这取决于dbQuery
变量的范围。当你在If- ElseIf-End If
内声明它时,它是这个条件的局部变量,在它不存在的条件之外。你必须使它成为整个函数的本地,所以你可以通过函数引用它。以下是更好的答案。
试试这个
Private Sub search()
'search method for Search Record'
Dim dbQuery As String
Try
If cmbKeywords.Text = "ID" Then
dbQuery = "Select * from tblstudents where id =" & txtSearch.Text
ElseIf cmbKeywords.Text = "Name" Then
dbQuery = "Select * from tblstudents where id =" & txtSearch.Text
ElseIf cmbKeywords.Text = "Address" Then
dbQuery = "Select * from tblstudents where id =" & txtSearch.Text
ElseIf cmbKeywords.Text = "Contact" Then
dbQuery = "Select * from tblstudents where id =" & txtSearch.Text
ElseIf cmbKeywords.Text = "Civil Status" Then
dbQuery = "Select * from tblstudents where id =" & txtSearch.Text
End If
Dim dbConnection As New MySqlConnection(dbConString)
Dim dbCmd As New MySqlCommand(dbQuery, dbConnection)
Dim dbReader As MySqlDataReader
dbConnection.Open()
dbReader = dbCmd.ExecuteReader()
dgvRecords.ReadOnly = True
dgvRecords.ColumnCount = 6
dgvRecords.Columns(0).Name = "Stud ID"
dgvRecords.Columns(1).Name = "Name"
dgvRecords.Columns(2).Name = "Address"
dgvRecords.Columns(3).Name = "Gender"
dgvRecords.Columns(4).Name = "Status"
dgvRecords.Columns(5).Name = "Contact No"
While dbReader.Read()
dgvRecords.Rows.Add(dbReader("id"), dbReader("lastname") & ", " & dbReader("firstname") & " " & dbReader("middlename"), dbReader("address"), dbReader("gender"), dbReader("civil_status"), dbReader("contact"))
End While
dbReader.Close()
dbConnection.Close()
Catch ex As Exception
MsgBox("ERROR: " & ErrorToString(), MsgBoxStyle.Critical)
End Try
End Sub