我有一个DataGridView
数据集的数据绑定。但是,我尝试运行查询而不是数据源,并更新DataGridView
以显示查询结果。
我使用的代码是:
Public Sub Call_Filter()
Dim myCon = New OleDbConnection(My.Settings.Database_string)
Try
myCon.Open()
Catch ex As Exception
MsgBox("Sorry, An Error Occurred" & vbNewLine & _
"Database Not Found!" & vbNewLine & vbNewLine & _
"Error Message: " & ex.Message, MsgBoxStyle.OkOnly, "Could Not Locate Database")
Main.VIEW_SavingMessage.Visible = False
Exit Sub
End Try
Dim FilterAdaptor = New OleDbDataAdapter
Dim FilterAdaptorText = "SELECT ID, [Name Of Person], [SAP Job Number], [Site Name], [Asset Description], [Spares Supplier], [Supplier Contact Name], [Supplier Contact Phone Number], " & _
"[Supplier Contact Email], [Spares Description], [Part Number], [Quantity To Order], Cost, Comments, [Request Date], [Date Ordered], [Ordered By], [Invoice Received], " & _
"[Invoice Paid], [Method Of Payment], [Date Item Received], [Quote Attatchment], [Marked As Urgent] " & _
"FROM Spares " & _
"WHERE ([Name Of Person] = @NameOfPerson OR @NameOfPerson = '' OR @NameOfPerson IS NULL) AND " & _
"([SAP Job Number] = @SAPJobNo OR @SAPJobNo = '' OR @SAPJobNo IS NULL)"
Dim FilteredDataSet = New DataSet
Dim sqr As OleDbCommand = New OleDbCommand(FilterAdaptorText, myCon)
sqr.Parameters.Add("@NameOfPerson", OleDbType.VarChar).Value = Main.Name_Of_PersonToolStripTextBox.Text
If Main.FilterJobNo.Text <> "" Then
sqr.Parameters.Add("@SAPJobNo", OleDbType.Integer).Value = Main.FilterJobNo.Text
Else
sqr.Parameters.Add("@SAPJobNo", OleDbType.Integer).Value = DBNull.Value
End If
Try
sqr.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Sorry, An error occured!" & vbNewLine & _
"The database rejected the request" & vbNewLine & vbNewLine & _
"Error Message: " & ex.Message, MsgBoxStyle.OkOnly, "Database Refused Query")
Exit Sub
End Try
FilterAdaptor.SelectCommand = sqr
FilterAdaptor.Fill(FilteredDataSet)
Main.DataGridView1.DataSource = FilteredDataSet
myCon.Close()
End Sub
虽然此代码执行正确,并且不会抛出任何错误,但它会向DataGridView
返回一个空行,而不是正确的过滤结果。我知道过滤器查询正常工作,因为我通过Access测试它并返回正确的信息。
我无法使用任何其他方法,因为我有一个大型查询要执行,这只是用于测试的过滤器查询的一小部分。
我遇到的问题似乎是尝试将过滤后的信息输入我的DataGridView
。我错过了什么基本的东西?我已经设法以类似的方法从数据库中插入,读取和删除,但是在使用WHERE命令时这似乎不起作用。
更新 - 2015年6月29日
我仍然试图让它发挥作用,但经过研究改变了我的方法:
Public Function getDataSet() As DataSet
Dim ds As New Data.DataSet
ds.Tables.Add(New DataTable("Filtered"))
Dim Con As New OleDb.OleDbConnection(My.Settings.Database_string)
Con.Open()
Dim ad As New Data.OleDb.OleDbDataAdapter("SELECT ID, [Name Of Person] " & _
"FROM Spares " & _
"WHERE [Name Of Person] = Joe", Con)
ad.Fill(ds.Tables("Filtered"))
Return ds
End Function
现在发生的事情是我收到以下错误:
没有给出一个或多个必需参数的值。
但我不明白这个假设的位置&#34;参数&#34;来自哪里?我还没有使用过任何参数?
由于
答案 0 :(得分:1)
您的查询的WHERE子句是错误的。
"WHERE ([Name Of Person] = @NameOfPerson OR @NameOfPerson = '' OR @NameOfPerson IS NULL) AND
因为参数/变量只是[Name of Person]
的一个条件。它应该只在WHERE中出现一次。如果该人的姓名是&#34; Bob&#34;,该部分将解析为:
"WHERE ([Name Of Person] = 'Bob' OR 'Bob'= '' OR 'Bob' IS NULL) AND ...
显然,&#34; Bob&#34;永远不会是空的或''
。这些都应该是列名:
"WHERE ([Name Of Person] = 'Bob' OR [Name Of Person]= '' OR [Name Of Person] IS NULL) AND ...
[SAP Job Number]
部分也是如此。
答案 1 :(得分:0)
您似乎无法绑定数据。设置数据源后尝试DataGridView1.Databind()
。
答案 2 :(得分:0)
我终于设法使用此功能:
Public Function getDataSet() As DataSet
Dim ds As New Data.DataSet
ds.Tables.Add(New DataTable("Filtered"))
Dim Con As New OleDb.OleDbConnection(My.Settings.Database_string)
Con.Open()
Dim ad As New Data.OleDb.OleDbDataAdapter
Dim cmd As New OleDbCommand("SELECT * FROM Spares " & _
"WHERE ([Name Of Person] = @NOP OR [Name Of Person] = '' OR [Name Of Person] IS NULL Or @NOP IS NULL) AND " & _
"([SAP Job Number] = @SJN OR [SAP Job Number] = '' OR [SAP Job Number] IS NULL Or @SJN IS NULL)", Con)
If Main.Name_Of_PersonToolStripTextBox.Text = "" Then
cmd.Parameters.AddWithValue("@NOP", DBNull.Value)
Else
cmd.Parameters.AddWithValue("@NOP", Main.Name_Of_PersonToolStripTextBox.Text)
End If
If Main.FilterJobNo.Text = "" Then
cmd.Parameters.AddWithValue("@SJN", DBNull.Value)
Else
cmd.Parameters.AddWithValue("@SJN", Main.FilterJobNo.Text)
End If
ad.SelectCommand = cmd
ad.Fill(ds.Tables("Filtered"))
Return ds
End Function
并使用它来调用它:
Private Sub FILTER_Accept_Button_Click(sender As Object, e As EventArgs) Handles FILTER_Accept_Button.Click
DataGridView1.DataSource = Database_Management_Codes.getDataSet().Tables(0)
End Sub