如何使用visual studio VB Code中的文本框和搜索按钮进行数据库搜索

时间:2015-12-06 05:14:13

标签: vb.net

我想在txtSearchCriteria文本框中键入内容,当我点击“执行搜索”按钮时,它会将其找到的所有记录显示在我在表单中的其他文本框中。我在Visual Studio 2012中使用带有数据集的localdb文件。到目前为止,除了搜索按钮之外,我还能正常工作。这是我项目中的完整代码。

'Import Namespaces for SQL
 Imports System.Data
 Imports System.Data.SqlClient

Public Class Form1 Dim objCurrencyManager As CurrencyManager

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Loads data into the 'CustomersDataSet.Customers' table.
    Me.CustomersTableAdapter.Fill(Me.CustomersDataSet.Customers)
    'Controls Record Number Movement
    objCurrencyManager = CType(Me.BindingContext(CustomersBindingSource), CurrencyManager)
    'Display the current record number 
    Position()
    'Display a tool tip at the bottom of screen
    lblToolStripLabel1.Text = "Ready To Compute Data"

End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    'Used try catch block to validate all fields to ensure proper use of form and to display error if required fields are missing user imput
    Try
        Me.Validate()
        Me.CustomersBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.CustomersDataSet)
        lblToolStripLabel1.Text = "Record Saved" 'Display that record has been saved 
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    Position()
End Sub

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    'code to update an existing record and display that redord was updated successfully
    Try
        Me.Validate()
        Me.CustomersBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.CustomersDataSet)
        lblToolStripLabel1.Text = "Record Updated" 'display record updated
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    Position()
End Sub


Private Sub dtnDelete_Click(sender As Object, e As EventArgs) Handles dtnDelete.Click
    ' Removes the current record and displays the new position in the database
    Me.CustomersBindingSource.RemoveCurrent()
    Position()
End Sub

Private Sub btnMoveFirst_Click(sender As Object, e As EventArgs) Handles btnMoveFirst.Click
    'Moves the current record to the begining of the dataset. The First Record
    Me.CustomersBindingSource.MoveFirst()
    Position()
End Sub

Private Sub btnMovePrevious_Click(sender As Object, e As EventArgs) Handles btnMovePrevious.Click
    'moves to the previous record and displays the position
    Me.CustomersBindingSource.MovePrevious()
    Position()
End Sub

Private Sub btnMoveNext_Click(sender As Object, e As EventArgs) Handles btnMoveNext.Click
    'Moves to the next record in the dataset
    Me.CustomersBindingSource.MoveNext()
    Position()
End Sub

Private Sub btnMoveLast_Click(sender As Object, e As EventArgs) Handles btnMoveLast.Click
    'Moves to the last Record in the Dataset
    Me.CustomersBindingSource.MoveLast()
    Position()
End Sub

Private Sub Position()
    'This code displays the positio of the current record the user is viewing and shows it out of the number of recors that are held in the dataset 
    txtRecordPosition.Text = objCurrencyManager.Position + 1 & " Of " & objCurrencyManager.Count()
End Sub

Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
    'Clears the form for user imput, Created a new record and adds the current date to CustomerDate field and adds the next CustomerNumber available.
    Me.CustomersBindingSource.AddNew()
    txtCustomerNumber.Text = Me.CustomersBindingSource.Count + 1
    txtCustomerDate.Text = Date.Today.ToShortDateString
End Sub

Private Sub btnPerformSort_Click(sender As Object, e As EventArgs) Handles btnPerformSort.Click
    'Code to Sort by different field within the dataset
    Select Case cboField.SelectedIndex
        Case 0 'CustomerNumber
            CustomersBindingSource.Sort = "CustomerNumber"
        Case 1 'First Name
            CustomersBindingSource.Sort = "FirstName"
        Case 2 'Last Name
            CustomersBindingSource.Sort = "LastName"
        Case 3 'City
            CustomersBindingSource.Sort = "City"
        Case 4 'Province
            CustomersBindingSource.Sort = "Province"
    End Select

    btnMoveFirst_Click(Nothing, Nothing)
    lblToolStripLabel1.Text = "Records Sorted"

End Sub

Private Sub btnPerformSearch_Click(sender As Object, e As EventArgs) Handles btnPerformSearch.Click
    'This will take the user imput from the txtSearchCriteria textbox and return all records that have the criteria 

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

这是一个查询数据库的示例,假设您正在连接到sql server。

Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("<your connection string name>").ConnectionString
Using con As New SqlConnection(constr)
    Using cmd As New SqlCommand()
        cmd.CommandText = "SELECT ContactName, City, Country FROM Customers WHERE ContactName LIKE '%' + @ContactName + '%'"
        cmd.Connection = con
        cmd.Parameters.AddWithValue("@ContactName", txtSearch.Text.Trim())
        Dim dt As New DataTable()
        Using sda As New SqlDataAdapter(cmd)
            sda.Fill(dt)
            gvCustomers.DataSource = dt
            gvCustomers.DataBind()
        End Using
    End Using
End Using
End Sub

要在第一步解释我们正在声明连接字符串,然后我们打开SqlConnection对象,然后我们根据输入的txtSearch值查询数据库表ContactName。接下来我们正在填写数据集并绑定网格。

我建议你在sql连接上阅读这个msdn article