用datagridview搜索问题 [英] Searching problem with datagridview

查看:70
本文介绍了用datagridview搜索问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vb.net 2005并访问2003

我有一个文本框,当我输入名称时,它会搜索数据库并在datagridview中显示它,而datagridview的列中会显示名称和价格.

我想知道的是,当我搜索必须在datagridview中显示的项目时,我一次能够搜索一个名称,但是我想要搜索的是将其放入datagridview中的名称,然后搜索另一个名称,并将其显示在第一个搜索下方.

这是我的代码:

I''m using vb.net 2005 and access 2003

I have a textbox that when I enter a name it searches through the database and displays it in the datagridview and the datagridview has columns that says name and price.

What I want to know is when I search for the item it has to show in the datagridview, I have been able to search for one name at a time but what I want is to search for a name have it put in the datagridview and then search for another name and have it displayed below the first search.

Here is my code:

Try
    If txtProduct.Text <> "" Then
        Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM [Herbal Crazy] WHERE [Name] = ''" + txtProduct.Text + "''", conn)
        Dim ds As DataSet = New DataSet()
        dataAdapter.Fill(ds, "Products")
        Dim table As DataTable = ds.Tables(0)

        ProductsDataGridView.DataSource = table
        ProductsDataGridView.Refresh()

    Else
        MessageBox.Show("Enter Product.")
    End If
Catch ex As Exception
    MessageBox.Show(ex.ToString)
Finally
    conn.Close()
End Try


我希望我能清楚地解释一切

我看过其他页面,但它们都使我感到困惑.

谢谢您.


I hope I explained everything clearly

I have looked at other pages but they all confuse me.

Thank you

推荐答案

请不要让字符串包含以生成SQL命令-这样会使您不愿遭受意外或故意的SQL注入,这可能会破坏数据库.例如,如果我输入
Please don''t concatentate strings to produce your SQL commands - it leaves you vunerable to accidental or deliberate SQL Injection, which could destroy your database. For example, if I was to type
x'';DROP TABLE [Herbal Crazy]

在您的文本框中并进行搜索,您的数据库将如何处理?注意:除非您有备份,否则请不要尝试!

请改用参数化查询.

in your text box and do a search, what would happen to your database? Note: don''t try it, unless you have a backup!

Use Parametrized queries instead.

Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM [Herbal Crazy] WHERE [Name] = @NM", conn)
dataAdapter.SelectCommand.Parameters.AddWithValue("@NM", txtProduct.Text)




对于您的搜索问题,我认为您无法真正做到,而不是您的想法.如果您使用DataAdapter,它将期望获得更新等信息,并将其写回数据库.您想要做的事情通过将可能与db中的记录重复的新记录添加到datagridview中而在某种程度上颠覆了这一点.

我认为您将必须手动执行-使用List作为DataGridView源,并从搜索返回的结果中填充它.然后,您可以将项目添加到列表中,并刷新DataGridView.




With your search problem, I don''t think you can really do it, not the way you think. If you use a DataAdapter, it is expecting to get updates and so forth, and to write those back to the database. What you want to do subverts that somewhat, by possibly adding new records to the datagridview that are duplicates of the records in the db already.

I think you will have to do it manually - use a List as the DataGridView source, and populate it from the results returned by your search. You can then add items to the list, and refresh the DataGridView.


您可以简单地将"Dim ds As DataSet = New DataSet()"移动到如下形式的公共声明中:
公共课程表格1
Dim ds As DataSet = New DataSet()
You can simply move "Dim ds As DataSet = New DataSet()" to public declarations of form like this:
Public Class Form1
Dim ds As DataSet = New DataSet()


我使用的方法直接在源代码中进行查询,并被证明对我要完成的工作更加具体.该方法的使用方法如下:

The method I use has the query directly in the source and has proven to be more specific to what I want to accomplish. The method is use is as follows:

<pre lang="vb">Imports System.Data.SqlClient<br />
Class Form1<br />
Dim connString As String = My.Settings.DatabaseConnectionName()<br />
Dim conn = New SqlConnection(connString)<br />
conn.Open()<br />
<br />
Dim sqlText As String<br />
sqlText = "SELECT * FROM tablename " & _<br />
"WHERE (first_name LIKE ''%" & TextBox.Text & "%'' OR last_name LIKE ''%" & TextBox.Text & "%'') " & _<br />
"ORDER BY last_name, first_name"<br />
Dim sqlStatement As New SqlCommand(sqlText, conn)<br />
Dim connInfo As SqlDataReader = sqlStatement.ExecuteReader()<br />
With DataGridView1.Rows<br />
    .Clear()<br />
    while connInfo.Read()<br />
        .Add(connInfo!first_name, _<br />
            connInfo!last_name)        <br />
    end while<br />
    connInfo.Close()<br />
End With<br />
<br />
conn.Close()<br />
End Class</pre><br />


这篇关于用datagridview搜索问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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