通过datagridview搜索值

时间:2015-11-13 20:30:43

标签: .net vb.net datagridview oledb

我尝试通过在文本框中输入文本然后使用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查询的XML文字有关,但无法弄清楚。

提前致谢。

1 个答案:

答案 0 :(得分:7)

通过一遍又一遍地创建New数据库对象,您将以自己的方式实现目标。如果DataAdapter是表单级变量,则必须编写更少的代码:

Public Class Form1
    ' declare some persistant DB objects
    Private myDT As DataTable
    Private myDA As OleDbDataAdapter
    Private myStudentsDataView As DataView

    Private dbConnStr As String = "(your connection string)"

这些只是声明,没有它们的实例(没有New)。但其中声明它们确定Scope。它们会一直存在,直到表单关闭(或者您使用Dim和/或New覆盖它们。表格加载:

' initialize the objects
Dim sql = "SELECT A, B, C, D... FROM Students"

' this is the ONLY place you use NEW 
' with these objects
myDT = New DataTable()

' The Adapter can create its own Connection 
'     and SelectCommand
myDA = New OleDbDataAdapter(sql, dbConnStr)

Dim myCB As New OleDbCommandBuilder(da)

' "teach" the DA how to Update and Add:
myDA.UpdateCommand = myCB.GetUpdateCommand
myDA.InsertCommand = myCB.GetInsertCommand
myDA.DeleteCommand = myCB.GetDeleteCommand

myDA.Fill(myDT)
myDA.FillSchema(myDT, SchemaType.Source)

myStudentsDataView = myDT.DefaultView
dgvStudents.DataSource = myStudentsDataView

DataAdapter需要连接对象才能工作,但是当注释提及而不是显式创建连接对象时,适配器可以创建自己的连接对象。它会根据需要打开和关闭它。 SelectCommand也是如此 - 它将从传递的SELECT SQL语句中创建自己的。

请注意,最好按照希望列显示在DataTable中的顺序指定每列。重要的是, DataAdapter知道如何删除,插入和更新行。 DataTable知道如何删除,插入和更新行。只要您不破坏它或替换它,您就不必编写任何SQL来添加或更改行!

在大多数情况下,DataSource用作DGV的myDGV.DataSource = myDT

DataTable

DGV将创建所需的列并将数据显示为行。当用户输入单元格时,这些更改会反映在DataGridView中,因此无需任何代码将其删除。

如果用户在myDa.Update(myDT) 中编辑数据,则只需将更改发送回数据库即可:

Private Sub AddStudent()
    ' no need to (RE)create DataAdapter

    ' add the data to a new row:
    Dim dr = myDT.NewRow
    dr.Item("FirstName") = textbox1.text
    dr.Item("LastName") = textbox2.text
    ' etc etc

    ' add the new row to the datatable
    myDT.Rows.Add(dr)
   ' with a persistent DA, this is all you need to add a row:
   myDA.Update(myDT)
End Sub

在这种情况下,根据以前的问题,数据来自文本控件而不是DGV。所以:

DataAdapter

我们教授&#34; myDA.Update(myDT)如何更新表单加载中的行,以便实际更新数据库(一旦数据在DT中)是一行代码:DataTable

myDA.Update(myDT)跟踪每一行是新的,更改的还是删除的,因此myDa.Fill(myDT) 会对每一行采取适当的措施。如果系统是多用户,您可以选择其他用户进行更改:

Private Sub Search(txt As String)
    myStudentsDataView.RowFilter = String.Format("LastName = '{0}'", txt)

搜索也很简单:

myStudentsDataView = myDT.DefaultView

删除过滤器:

DataAdapter

如果/ New无法添加,插入,更新或删除,则意味着您在某处创建了myDataView。不要这样做。同样,myDT会显示RowFilter中的内容,直到您创建新的DT或DV或更改Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'soft_oal': libsoft_oal.so: cannot open shared object file: No such file or directory at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:169) at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:242) at com.sun.jna.Library$Handler.<init>(Library.java:140) at com.sun.jna.Native.loadLibrary(Native.java:368) at com.sun.jna.Native.loadLibrary(Native.java:353) at org.urish.openal.jna.ALFactory.<init>(ALFactory.java:16) at org.urish.openal.OpenAL.<init>(OpenAL.java:24) at defaultpackage.SoundTest.<init>(SoundTest.java:22) at defaultpackage.SoundTest.main(SoundTest.java:17) :run FAILED FAILURE: Build failed with an exception.