我尝试通过在文本框中输入文本然后使用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文字有关,但无法弄清楚。
提前致谢。
答案 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.
。