在数据库中按名称搜索记录并在gridview中显示 [英] Search record by name in database and display in gridview

查看:58
本文介绍了在数据库中按名称搜索记录并在gridview中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1个表名finalsort,它有列lotno,名称,类型,大小等。



i希望按名称搜索数据并在gridview中显示它名字。



有谁能告诉我怎么做?



i不知道代码。


如果有人可以提前感谢
,那么
将被罚款。



sudeshna

I have 1 table name finalsort which has columns lotno,name,type,size etc.

i want to search data by name and display it in gridview based on name.

can anyone tell me how to do it?

i dont know the code.

will be obliged if anyone can help
thanks in advance.

sudeshna

推荐答案

假设你有一个按钮,点击它从数据库中检索的数据并填写gridview,然后在按钮点击事件中写下以下代码。



我正在使用sqlserver数据库



Suppose you have button by clicking on it data retrieved from database and fill in gridview then write folowing code in button click event.

I am using sqlserver database

dim str as String="provide name for search"
Dim cn As New SqlConnection("<your connection="" string="">")
       Dim cmd As New SqlCommand("Select * from finalsort where name='" + str + "'", cn)
       cn.Open()
 
       Dim da As New SqlDataAdapter(cmd)
       Dim ds As New DataSet
       da.Fill(ds)
DataGridView1.DataSource=ds.Tables(0)
DataGridView1.Refresh() 'If required
</your>


Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        'Dim strcon As String = ("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
        Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
        'Dim da As New SqlDataAdapter
        Dim cmd As New SqlCommand
        Dim ds As New DataSet
       


        'Try

        cmd.Connection = cn
        cn.Open()

        Dim da As New SqlDataAdapter("select * from finalsort where name='" & ComboBox1.SelectedItem & "'", cn)
       

        ds = New DataSet
        da.Fill(ds)

FinalsortDataGridView.DataSource = ds.Tables(0)
FinalsortDataGridView.Refresh()
        cn.Close()
    End Sub


此代码在我的情况下运行正常,没有任何问题:



This code working fine in my case without any problem:

Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim constr As String = "<your connection="" string="">"
        Dim cn As New SqlConnection(constr)
        cn.Open()
        Dim da As New SqlDataAdapter("Select * from Table1 where name='" & ComboBox1.SelectedItem & "'", cn)
        Dim ds As New DataSet()
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        DataGridView1.Refresh()
        cn.Close()
    End Sub</your>


这篇关于在数据库中按名称搜索记录并在gridview中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆