在VB.net中的MS Access / DataGridView上搜索数​​据(字符串)的最有效方法是什么

时间:2019-01-20 22:34:39

标签: vb.net

如果匹配变量的布尔值等于true,从数据库/ DGV中搜索数据并与textbox.text比较的最有效方法是什么

我的行数为500以上

我使用For Next和表适配器过滤器

'adapter filter
Me.StudenttableBindingSource.Filter = "[Card Number] like '" & IDTapTextBox.Text & "'"

'textbox1 is bind to [Card Number]
Dim found As Boolean
if textbox1.text = IDTapTextBox.Text then
found = true
End

'For Next
Dim found As Boolean
            Dim lookfor As String = Student_numberTextBox.Text
            For dgv As Integer = In_TableDataGridView.Rows.Count - 1 To 0 Step -1
                If In_TableDataGridView.RowCount > 0 Then
                    If lookfor = In_TableDataGridView.Rows(dgv).Cells(0).Value.ToString Then
                        found = True
                    End If
                Else
                    found = False
                End If
    enter code here
            Next

1 个答案:

答案 0 :(得分:0)

您是指要访问Access的文本框吗?如果是这样,请尝试这个。

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private bindingSource1 As New BindingSource()
    Private dataAdapter As New OleDbDataAdapter()

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Excel\Desktop\Northwind_2012.mdb"
        Dim selectCommand As String
        Dim connection As New OleDbConnection(connectionString)

        selectCommand = "Select * From MyExcelTable ORDER BY ID"
        Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

        With DataGridView1
            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
        End With

        Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
        Dim table As New DataTable()

        table.Locale = System.Globalization.CultureInfo.InvariantCulture

        Me.dataAdapter.Fill(table)
        Me.bindingSource1.DataSource = table

        Dim data As New DataSet()

        data.Locale = System.Globalization.CultureInfo.InvariantCulture

        DataGridView1.DataSource = Me.bindingSource1
        Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
        Me.DataGridView1.AutoResizeColumns( _
            DataGridViewAutoSizeColumnsMode.AllCells)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, btnUpdate.Click
        Dim table As New DataTable()

        Me.bindingSource1 = Me.DataGridView1.DataSource
        table = Me.bindingSource1.DataSource

        Me.dataAdapter.Update(table)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, btnClose.Click
        Me.Close()
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.Click

        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Excel\Desktop\Northwind_2012.mdb"
        Dim selectCommand As String
        Dim connection As New OleDbConnection(connectionString)

        'selectCommand = "Select * From MyExcelTable where Fname = '" & TextBox1.Text & "'"
        '"SELECT * FROM Customers WHERE Address LIKE '" & strAddressSearch & "%'"
        'or ending with:
        '"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearch & "'"

        selectCommand = "Select * From MyExcelTable where Fname Like '%" & TextBox1.Text & "%'"
        Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

        With DataGridView1
            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
        End With

        Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
        Dim table As New DataTable()

        table.Locale = System.Globalization.CultureInfo.InvariantCulture

        Me.dataAdapter.Fill(table)
        Me.bindingSource1.DataSource = table

        Dim data As New DataSet()

        data.Locale = System.Globalization.CultureInfo.InvariantCulture

        DataGridView1.DataSource = Me.bindingSource1
        Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
        Me.DataGridView1.AutoResizeColumns( _
            DataGridViewAutoSizeColumnsMode.AllCells)

    End Sub
End Class

我还提供了一些其他功能,以在FormLoad事件上从Access读取并将结果保存在DataGridView中以供Access使用。希望对您有所帮助。