我试图通过在文本框中输入文本,然后使用SQL查询数据库,然后在datagridview中显示结果,在数据库中搜索特定值。
这是代码:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
Connection.Open()
Dim dataTable As New DataTable
Dim dataSet As New DataSet
dataSet.Tables.Add(dataTable)
Dim dataAdapter As New OleDbDataAdapter
Dim SQLQuery As String
SQLQuery = <sql>
SELECT *
FROM Students
WHERE StudentFirstName = @StudentFirstName
</sql> .Value
dataAdapter = New OleDbDataAdapter(SQLQuery, Connection)
dataAdapter.SelectCommand.Parameters.Add("@StudentFirstName", SqlDbType.Text).Value = txtStudentFirstname.Text
dataAdapter.Fill(dataTable)
dgrStudentDatabaseViewer.DataSource = dataTable.DefaultView
ShowItems()
Connection.Close()
End Sub
调用ShowItems()将刷新datagridview,这是它的代码
Private Sub ShowItems() ' the following delcleration are used for displaying the contents of the table
Dim dataAdapter As New OleDbDataAdapter
Dim DataTable As New DataTable
Dim DataSet As New DataSet
Dim SQLQuery As String = <sql>SELECT * FROM Students</sql>
DataSet.Tables.Add(DataTable)
dataAdapter = New OleDbDataAdapter(SQLQuery, Connection)
dataAdapter.Fill(DataTable) ' fills the content from the database into the table in vb net
dgrStudentDatabaseViewer.DataSource = DataTable.DefaultView
Connection.Close()
End Sub
此刻,当我尝试搜索时,什么也没有发生,datagridview的内容仍然保持不变。我认为这可能与我的SQL Query的XML文字有关,但无法弄清楚。
提前致谢。
素胚勾勒不出你
慕莱坞森