每当我搜索我的数据库时,它总是说"对象引用未设置为对象的实例。" 请有人帮帮我。谢谢。
以下是我的搜索代码:
Private Sub BtnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSearch.Click
If TBSearch.Text = "" Then
MsgBox("Please enter keyword to search...", MsgBoxStyle.Information, "Keyword to search...")`
TBSearch.Focus()
Exit Sub
End If
Call OpenConnection()
With OleDa
Call Initialized()
.SelectCommand = New OleDb.OleDbCommand() <-------- Error appears here.
.SelectCommand.CommandText = "SELECT * FROM [HighSchool] WHERE [LastName] Like '%%" & TBSearch.Text & "%%' or [FirstName] Like '%%" & TBSearch.Text & "%%' or [MI] Like '%%" & TBSearch.Text & "%%'" & _
"Or [Gender] Like '%%" & TBSearch.Text & "%%' or [Address] Like '%%" & TBSearch.Text & "%%' or [Birthday] Like '%%" & TBSearch.Text & "%%' or [RNumber] Like '%%" & TBSearch.Text & "%%'" & _
"Or [ENumber] Like '%%" & TBSearch.Text & "%%' ORDER By LastName ASC"
.SelectCommand.Connection = OleCn
Call PopulateListView()
If Me.LV.Items.Count >= 1 Then
MsgBox(Me.LV.Items.Count & " Record(s) found for " & "( " & Me.TBSearch.Text & " )", MsgBoxStyle.OkOnly, "Record(s) found...")
Else
MsgBox("No record(s) found for " & "( " & Me.TBSearch.Text & " )" & " , please try again... ", MsgBoxStyle.Critical, "No record found...")
TBSearch.Focus()
TBSearch.SelectAll()
End If
End With
Call CloseConnection()
End Sub
这是我的Populate ListView代码:
Public Sub PopulateListView()
Me.LV.Items.Clear()
Dim OleDr As OleDb.OleDbDataReader
OleDr = OleDa.SelectCommand.ExecuteReader()
Do While OleDr.Read()
Dim Item As New ListViewItem
Item.Text = IIf(OleDr.IsDBNull(0), "", OleDr.Item(0))
For shtCntr = 1 To OleDr.FieldCount() - 1
If Not OleDr.IsDBNull(shtCntr) Then
Item.SubItems.Add(OleDr.Item("FirstName"))
Item.SubItems.Add(OleDr.Item("LastName"))
Item.SubItems.Add(OleDr.Item("MI"))
Item.SubItems.Add(OleDr.Item("Gender"))
Item.SubItems.Add(OleDr.Item("Address"))
Item.SubItems.Add(OleDr.Item("Birthday"))
Item.SubItems.Add(OleDr.Item("RNumber"))
Item.SubItems.Add(OleDr.Item("ENumber"))
Else
Item.SubItems.Add("")
End If
Next shtCntr
Me.LV.Items.Add(Item)
Loop
End Sub
答案 0 :(得分:2)
好的,我们来解决一些问题。
Call
。这只是VB 6的延续(即90年代后期)Exit Sub
。请改用Return
。 (与#1相同的原因)使用参数化SQL而不是字符串连接。你的代码按原样不起作用是有人搜索“O'Mally”。
Private Sub BtnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSearch.Click
If TBSearch.Text = "" Then
MsgBox("Please enter keyword to search...", MsgBoxStyle.Information, "Keyword to search...")`
TBSearch.Focus()
Exit Sub
End If
Call OpenConnection()
With OleDa
Call Initialized()
.SelectCommand = New OleDb.OleDbCommand()
.SelectCommand.CommandText = "SELECT * FROM [HighSchool] WHERE [LastName] Like @TBSearch or [FirstName] Like @TBSearch or [MI] Like @TBSearch Or [Gender] Like @TBSearch or [Address] Like @TBSearch or [Birthday] Like @TBSearch or [RNumber] Like @TBSearch Or [ENumber] Like @TBSearch ORDER By LastName ASC"
.SelectCommand.Connection = OleCn
.SelectCommand.Parameters.AddWithValue("@TBSearch", "%" & TBSearch.Text & "%")
Call PopulateListView()
If Me.LV.Items.Count >= 1 Then
MsgBox(Me.LV.Items.Count & " Record(s) found for " & "( " & Me.TBSearch.Text & " )", MsgBoxStyle.OkOnly, "Record(s) found...")
Else
MsgBox("No record(s) found for " & "( " & Me.TBSearch.Text & " )" & " , please try again... ", MsgBoxStyle.Critical, "No record found...")
TBSearch.Focus()
TBSearch.SelectAll()
End If
End With
Call CloseConnection()
End Sub
至于您的错误,唯一可能的原因是OleDa
是Nothing
。