如何使用组合框和文本框在vb.net中搜索数据 [英] How to search data in vb.net using combo box and textbox
问题描述
我有1个表名项,其中列是broker_id,name,auction_name,date,year,lot_no。
现在我已将所有这些列放在组合框中收藏。
现在我希望当用户点击组合框中的任何一列时。
例如lot_no
然后用户将在文本框中键入任何特定的lotno并单击搜索按钮,然后它将在网格视图中显示数据库中是否存在特定的lotno是不是。
请有人帮我提供代码吗?
或任何人都可以建议我用于搜索的其他好的或互动的方法。
我可以在这里使用ajax吗?
矿山项目是Windows应用程序,我正在使用visual studio ultimate 2010和sql server 2012.
请帮助。
Private Sub Button1_Click(sender As System.Object,e As System.EventArgs)处理Button1.Click
Dim str As String =(Data Source = .\INSTANCE; initial catalog = example; user = sa; password = gariahat)
Dim con As New SqlConnection (str)
Dim cmd As New SqlCommand(select * from item where broker_id like'%+ Trim(TextBox1.Text)+%',con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
If(da.Fill(ds,item))then
ItemDataGridView.DataSource = ds.Tables(0)
MessageBox.Show(匹配找到)
否则
MessageBox.Show(找不到匹配)
结束如果
结束子
你不能指望完全相同的东西,因为每个人都在特定的领域工作。检查这些2,你会得到一个想法:
VB.NET - 使用文本框和组合框搜索功能 [ ^ ]
使用组合和文本框搜索数据库 [ ^ ]
< b>编辑:
执行以下操作:
Dim SearchText As String = TextBox1.Text
Dim cmd As 新 SqlCommand( SELECT * FROM item WHERE broker_id与'%'& SearchText& %',con)
并根据此编辑您的下一个查询。
Imports System.Data.SqlClient
Imports System.Data
Public类Form6
Dim cn As New SqlConnection(Data Source = .\INSTANCE; initial catalog = example; user = sa; password = gariahat)
Dim da As New SqlDataAdapter
Dim cmd As New SqlCommand
'Dim dr As New SqlDataReader
Dim dt1 As DataTable
Dim ds As New DataSet
Private Sub ItemBindingNavigatorSaveItem_Click(sender As System.Object,e As System.EventArgs)处理ItemBindingNavigatorSaveItem.Click
Me.Validate()
Me.ItemBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.ExampleDataSet)
End Sub
Private Sub Form6_Load(sender As System.Object,e As System.EventArgs)Handles MyBase.Load
'TODO:Th是一行代码将数据加载到'ExampleDataSet.item'表中。您可以根据需要移动或删除它。
'Me.ItemTableAdapter.Fill(Me.ExampleDataSet.item)
结束子
私人子搜索()
Dim str As String =(Data Source = .\INSTANCE; initial catalog = example; user = sa; password = gariahat)
Dim con As New SqlConnection(str)
如果是ComboBox1。 SelectedIndex = 0然后
Dim searchtext As String = TextBox1.Text
Dim cmd As New SqlCommand(select * from item where broker_id like'%& searchtext&%',con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
If(da.Fill(ds,item))然后
ItemDataGridView.DataSource = ds.Tables(0)
MessageBox.Show(匹配找到)
否则
MessageBox.Show(找不到匹配)
结束如果
ElseIf ComboBox1.SelectedIndex = 1则
Dim searchtext As String = TextBox1.Text
Dim cmd As New SqlCommand(select * from item where name like'%& searchtext& %'由broker_id订购,con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
If(da.Fill (ds,item))然后
ItemDataGridView.DataSource = ds.Tables(0)
MessageBox.Show(匹配找到)
其他
MessageBox.Show(未找到匹配)
结束如果
ElseIf ComboBox1.SelectedIndex = 2则
Dim searchtext As String = TextBox1.Text
Dim cmd As New SqlCommand(select * from item where lot_no like'%& searchtext&%'order by broker_id,con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
If(da.Fill(ds,item))Then
ItemDataGridView.DataSource = ds.Tables(0)
MessageBox.Show(匹配找到)
El se
MessageBox.Show(找不到匹配)
结束如果
ElseIf ComboBox1.SelectedIndex = 3则
Dim searchtext As String = TextBox1 .Text
Dim cmd As New SqlCommand(select * from item where where like'%& searchtext& %'由broker_id订购,con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
If(da.Fill (ds,item))然后
ItemDataGridView.DataSource = ds.Tables(0)
MessageBox.Show(匹配找到)
否则
MessageBox.Show(找不到匹配)
结束如果
ElseIf ComboBox1.SelectedIndex = 4那么
Dim searchtext As String = TextBox1.Text
Dim cmd As New SqlCommand(select * from item where year like'%& searchtext&%'order by broker_id,con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
If(da.Fill(ds,item))Then
ItemDataGridView.DataSource = ds.Tables(0)
MessageBox.Show(匹配找到)
Els e
MessageBox.Show(未找到匹配)
结束如果
结束如果
结束Sub
Private Sub Button1_Click(sender As System.Object,e As System.EventArgs)Handles Button1.Click
search()
End Sub
End Class
I have 1 table name item where columns are broker_id,name,auction_name,date,year,lot_no.
Now i have put all these columns in combo box collection.
Now i want as the user clicks on any one of the columns in combo box.
e.g lot_no
then the user will type any particular lotno in textbox and click on search button,then it will display in grid view whether that particular lotno is there in database or not.
Please can anyone help me with code?
or can any one suggest me any other good or interactive method to use for search.
Can i use ajax here?
Mine project is windows application and i am using visual studio ultimate 2010 and sql server 2012.
Please kindly help.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim str As String = ("Data Source=.\INSTANCE;initial catalog=example;user=sa;password=gariahat")
Dim con As New SqlConnection(str)
Dim cmd As New SqlCommand("select * from item where broker_id like '%" + Trim(TextBox1.Text) + "%'", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
If (da.Fill(ds, "item")) Then
ItemDataGridView.DataSource = ds.Tables(0)
MessageBox.Show("match found")
Else
MessageBox.Show("match not found")
End If
End Sub
You can't expect exactly same things because everyone works in particular domain.Check these 2,you will get an idea:
VB.NET - Search function using Textbox and Combo Box[^]
search database using combo and text box[^]
EDIT:
Do something like:
Dim SearchText As String = TextBox1.Text Dim cmd As New SqlCommand("SELECT * FROM item WHERE broker_id Like '%" & SearchText & "%' ", con)
and edit your next query also according to this.
Imports System.Data.SqlClient Imports System.Data Public Class Form6 Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=example;user=sa;password=gariahat") Dim da As New SqlDataAdapter Dim cmd As New SqlCommand 'Dim dr As New SqlDataReader Dim dt1 As DataTable Dim ds As New DataSet Private Sub ItemBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles ItemBindingNavigatorSaveItem.Click Me.Validate() Me.ItemBindingSource.EndEdit() Me.TableAdapterManager.UpdateAll(Me.ExampleDataSet) End Sub Private Sub Form6_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'ExampleDataSet.item' table. You can move, or remove it, as needed. 'Me.ItemTableAdapter.Fill(Me.ExampleDataSet.item) End Sub Private Sub search() Dim str As String = ("Data Source=.\INSTANCE;initial catalog=example;user=sa;password=gariahat") Dim con As New SqlConnection(str) If ComboBox1.SelectedIndex = 0 Then Dim searchtext As String = TextBox1.Text Dim cmd As New SqlCommand("select * from item where broker_id like '%" & searchtext & "%'", con) Dim da As New SqlDataAdapter(cmd) Dim ds As New DataSet() If (da.Fill(ds, "item")) Then ItemDataGridView.DataSource = ds.Tables(0) MessageBox.Show("match found") Else MessageBox.Show("match not found") End If ElseIf ComboBox1.SelectedIndex = 1 Then Dim searchtext As String = TextBox1.Text Dim cmd As New SqlCommand("select * from item where name like '%" & searchtext & "%' order by broker_id", con) Dim da As New SqlDataAdapter(cmd) Dim ds As New DataSet() If (da.Fill(ds, "item")) Then ItemDataGridView.DataSource = ds.Tables(0) MessageBox.Show("match found") Else MessageBox.Show("match not found") End If ElseIf ComboBox1.SelectedIndex = 2 Then Dim searchtext As String = TextBox1.Text Dim cmd As New SqlCommand("select * from item where lot_no like '%" & searchtext & "%'order by broker_id", con) Dim da As New SqlDataAdapter(cmd) Dim ds As New DataSet() If (da.Fill(ds, "item")) Then ItemDataGridView.DataSource = ds.Tables(0) MessageBox.Show("match found") Else MessageBox.Show("match not found") End If ElseIf ComboBox1.SelectedIndex = 3 Then Dim searchtext As String = TextBox1.Text Dim cmd As New SqlCommand("select * from item where place like '%" & searchtext & "%' order by broker_id", con) Dim da As New SqlDataAdapter(cmd) Dim ds As New DataSet() If (da.Fill(ds, "item")) Then ItemDataGridView.DataSource = ds.Tables(0) MessageBox.Show("match found") Else MessageBox.Show("match not found") End If ElseIf ComboBox1.SelectedIndex = 4 Then Dim searchtext As String = TextBox1.Text Dim cmd As New SqlCommand("select * from item where year like '%" & searchtext & "%' order by broker_id", con) Dim da As New SqlDataAdapter(cmd) Dim ds As New DataSet() If (da.Fill(ds, "item")) Then ItemDataGridView.DataSource = ds.Tables(0) MessageBox.Show("match found") Else MessageBox.Show("match not found") End If End If End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click search() End Sub End Class
这篇关于如何使用组合框和文本框在vb.net中搜索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!