如何使用选择查询中的数据集填充datagridview?

时间:2019-04-25 08:50:46

标签: vb.net

我正在使用SELECT查询创建搜索栏,我想将结果传输到DataGridView。但是我总是得到一个空的DataGridView。

查询没有问题,我已经尝试在访问中手动输入它。我在代码中做错了什么?在这里:

Using conn = New OleDbConnection(connstring)
    Try
        Dim Sql As String = "SELECT * FROM Products WHERE [Product Name] LIKE '" & txtSearchProduct.Text & "*'"

        Dim da As New OleDbDataAdapter(Sql, conn)
        Dim ds As New DataSet
        da.Fill(ds)
        DataGridView2.DataSource = ds.Tables(0)

    Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Error")
    End Try
End Using

4 个答案:

答案 0 :(得分:1)

问题如我在评论中所述。 ADO.Net使用%之类的东西来维护与大多数主要Sql引擎的一致性。但是我想指出,您的查询是不安全的,并且会受到SQL注入的影响,因此我提供了一个示例代码,其中使用参数将用户输入传递给命令。另外请注意,OleDbDataAdapter可以在Using语句中声明,就像使用OleDbConnection一样。请注意,如果您需要扩展数据集(ds)的范围,计划用它做其他事情。

Using conn As OleDbConnection = New OleDbConnection(connstring)
    Try
        Dim Sql As String = "SELECT * FROM Products WHERE [Product Name] LIKE @Product"

        Using da As OleDbDataAdapter = New OleDbDataAdapter(Sql, conn)
            da.SelectCommand.Parameters.Add("@Product", OleDbType.Varchar).Value = txtSearchProduct.Text & "%"
            Dim ds As New DataSet
            da.Fill(ds)
            DataGridView2.DataSource = ds.Tables(0)
        End Using
    Catch ex As Exception
        MsgBox(ex.ToString, MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Error")
    End Try
End Using

答案 1 :(得分:1)

就像Charles提到的,使用参数总是更好。我的答案有点不同,它使用的是读取器而不是适配器,以及数据表而不是整个数据集。仅当您打算写回表或典型情况包括绑定过程时,才应使用适配器。当您有多个表并且需要关联它们时,通常使用DataSet。另外请注意,无论您在搜索列中的位置如何匹配字符串,都很可能希望在参数中使用前一个%。

    Try
        Using conn = New OleDbConnection("YourConnString")
            conn.Open()

            Dim Cmd As New OleDbCommand("SELECT * FROM Products WHERE [Product Name] LIKE @Product", conn)
            Cmd.Parameters.AddWithValue("@Product", "'%" & txtSearchProduct.Text & "%'")
            Dim ProductsRDR As OleDbDataReader = Cmd.ExecuteReader

            Dim DTable As New DataTable With {.TableName = "Products"}
            DTable.Load(ProductsRDR)

            DataGridView1.DataSource = DTable


            conn.Close()
        End Using
    Catch ex As Exception
        MsgBox(ex.ToString, MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Error")
    End Try

答案 2 :(得分:0)

您要做的就是将Sql字符串中的*符号替换为%,就像这样:

这是您错误的Sql字符串:

Dim Sql As String = "SELECT * FROM Products WHERE [Product Name] LIKE '" & txtSearchProduct.Text & "*'"

将其更改为此:

Dim Sql As String = "SELECT * FROM Products WHERE [Product Name] LIKE '" & txtSearchProduct.Text & "%'"

enter image description here

答案 3 :(得分:0)

这应该涵盖那里的大多数常见情况。

Imports System.Data.SqlClient
Public Class Form1
    Dim sCommand As SqlCommand
    Dim sAdapter As SqlDataAdapter
    Dim sBuilder As SqlCommandBuilder
    Dim sDs As DataSet
    Dim sTable As DataTable

    Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click
        Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
        Dim sql As String = "SELECT * FROM Stores"
        Dim connection As New SqlConnection(connectionString)
        connection.Open()
        sCommand = New SqlCommand(sql, connection)
        sAdapter = New SqlDataAdapter(sCommand)
        sBuilder = New SqlCommandBuilder(sAdapter)
        sDs = New DataSet()
        sAdapter.Fill(sDs, "Stores")
        sTable = sDs.Tables("Stores")
        connection.Close()
        DataGridView1.DataSource = sDs.Tables("Stores")
        DataGridView1.ReadOnly = True
        save_btn.Enabled = False
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

    End Sub

    Private Sub new_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_btn.Click
        DataGridView1.[ReadOnly] = False
        save_btn.Enabled = True
        new_btn.Enabled = False
        delete_btn.Enabled = False
    End Sub

    Private Sub delete_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete_btn.Click
        If MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
            sAdapter.Update(sTable)
        End If
    End Sub

    Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
        sAdapter.Update(sTable)
        DataGridView1.[ReadOnly] = True
        save_btn.Enabled = False
        new_btn.Enabled = True
        delete_btn.Enabled = True
    End Sub
End Class