我很难理解为什么我的记录数量与查询语句不同。现在作为一个新用户,我显然没有正确编码,但看不到它。如果我在访问中运行我的查询它返回正确数量的记录,但在vb.Net中,它返回一个完全不同的数字。
例如,在我的代码中
Sub filllistview()
Try
'creatconn()
oledbCnn.ConnectionString = My.Settings.storageConnectionString
oledbCnn.Open()
'Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP 100 * from Postings WHERE Customer = '" & newvar & "' ORDER BY Date DESC", oledbCnn)
'dr = oledbCmd.ExecuteReader()
'drcount = Convert.ToInt32(dr("RowCount"))
Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP 100 * from Postings " & _
"WHERE Customer = ? ORDER BY Date DESC", oledbCnn)
oledbCmd.Parameters.AddWithValue("@p1", newvar)
Using dr = oledbCmd.ExecuteReader()
'clear items in the list before populating with new values
ListView1.Items.Clear()
While dr.Read()
ListView1.Items.Add(CDate(dr(4)).ToShortDateString()).UseItemStyleForSubItems = False
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1).ToString())
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(11).ToString())
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(7).ToString())
With ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CDbl(dr(5)).ToString("C"))
If CDbl(dr(5)) < 0 Then
.ForeColor = Color.Red
.BackColor = Color.Gainsboro
'.Font = New Font(Font.FontFamily, Font.Size, FontStyle.Bold)
Else
.ForeColor = tmpColor
End If
End With
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(14).ToString())
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3).ToString())
count += 1
End While
End Using
MessageBox.Show(CStr(ListView1.Items.Count - 1))
MessageBox.Show(count.ToString)
'If (count < 100) Then
' MessageBox.Show("less than 100")
'Else
' MessageBox.Show(count.ToString)
'End If
'autosize each column in listview
For Each col As ColumnHeader In ListView1.Columns
col.Width = -2
Next col
'refresh the list with new data
ListView1.Refresh()
'Enable the posting previous button
'btnNextPostings.Enabled = True
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'dr.Close()
oledbCnn.Close()
End Try
'btnNextPostings.Enabled = ListView1.Items.Count > 100
gbxPostings.Text = "Client Postings for: " & "( " & newvar & " )" & " Total Records: " & count
If (ListView1.Items.Count < 100) Then
btnNextPostings.Enabled = False
btnNextPostings.Text = "No data to show"
Else
btnNextPostings.Enabled = True
btnNextPostings.Text = "Next " & CStr(ListView1.Items.Count - 1) & " Postings"
End If
End Sub
我哪里出错了。非常感谢
答案 0 :(得分:1)
您似乎忘记在命令上调用ExecuteReader,因此,如果读取器实例已由先前的命令初始化,则您正在循环先前加载的内容。
Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP 100 * from Postings " & _
"WHERE Customer = ? ORDER BY [Date] DESC", oledbCnn)
oledbCmd.Parameters.AddWithValue("@p1", newvar)
Using OleDbDataReader dr = oledbCmd.ExecuteReader()
While dr.Read()
....
End While
End Using
我还将命令文本从字符串连接更改为参数化查询。使用参数化查询来避免sql注入和解析问题非常重要。另一个变化是围绕一次性对象Using Statement(最重要的是连接,但在代码中不清楚如何初始化它)。最后,当您连接到Access数据库时,DATE这个词是OleDbProvider使用的Acces Jet Engine的保留关键字。你需要在这个词周围加上方括号。
当然,如果你想要精确计算ListView中的项目,你应该在开始阅读循环之前通过调用ListView.Items.Clear()删除(清除)以前的内容,并确保必须{{} 1}}全局变量正确初始化为零。