2015-12-06 45 views
-1

我想在我txtSearchCriteria文本框中輸入的東西做一個數據庫搜索,當我打的執行搜索按鈕,它會顯示它找到的所有記錄到另一個文本框我有我的形式。我在Visual Studio 2012中使用了一個帶有數據集的localdb文件。到目前爲止,除了搜索按鈕外,其他所有工作都已經完成。這是我的項目中的全部代碼。如何使用文本框,並在Visual Studio VB代碼搜索按鈕

'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 
+0

什麼阻止你查詢基於文本框值的結果。 –

+0

我以前從來沒有用過SQL,最後一分鐘我被拋到了這裏,並說出了它的數字 – BiohazzardXIII

回答

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值查詢數據庫表聯繫人姓名inputted.Next我們填充所述數據集,並結合說明格。

我建議你在sql連接上讀這個msdn article

相關問題