如何使用vb.net中的数据库中的文本框搜索数据 [英] how to search data using textbox in database in vb.net

查看:133
本文介绍了如何使用vb.net中的数据库中的文本框搜索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,但是当我点击搜索时出现错误!请帮助..

谢谢



< pre lang =vb> 





Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button1.Click



ds =新数据集

tables = ds.Tables

da =新OleDbDataAdapter(选择*来自AMORLITO,其中_name ='& TextBox1。文本&',con)'将项目更改为您的数据库名称



如果da.Fill(ds,AMORLITO)则'将项目更改为您的数据库name

Dim view As New DataView(tables(0))

source1.DataSource = view

DataGridView1.DataSource = view

Else

MsgBox(没有数据!)

结束如果

结束Sub

解决方案

首先修复 SQL注入 [ ^ ]代码中的漏洞。



然后,检查您的列名称。它真的叫做 _name (带有前导下划线字符)?如果是这样,你需要将它包在方括号中。



尝试这样的事情:

  Dim  da  As   OleDbDataAdapter(< span class =code-string>  SELECT * FROM AMORLITO WHERE [_ name] =?,con)

' OleDb不使用命名参数,因此参数名称无关紧要:
da.SelectCommand.Parameters.AddWithValue( p0,TextBox1.Text)

Dim ds As DataSet ()
如果 da.Fill(ds, AMORLITO然后
Dim 视图 As DataView = ds.Tables( 0 )。DefaultView
source1.DataSource = view
DataGridView1.DataSource = view
Else
MsgBox( NO DATA!
结束 如果


this is my code but when I click on search there's an error! Please help..
thanks

<pre lang="vb">



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from AMORLITO where _name = '" & TextBox1.Text & "'", con) 'Change items to your database name

If da.Fill(ds, "AMORLITO") Then 'Change items to your database name
Dim view As New DataView(tables(0))
source1.DataSource = view
DataGridView1.DataSource = view
Else
MsgBox("NO DATA!")
End If
End Sub

解决方案

Start by fixing the SQL Injection[^] vulnerability in your code.

Then, check your column name. Is it really called _name (with a leading underscore character)? If so, you'll need to wrap it in square brackets.

Try something like this:

Dim da As New OleDbDataAdapter("SELECT * FROM AMORLITO WHERE [_name] = ?", con)

' OleDb doesn't use named parameters, so the parameter name doesn't matter here:
da.SelectCommand.Parameters.AddWithValue("p0", TextBox1.Text)

Dim ds As New DataSet()
If da.Fill(ds, "AMORLITO") Then
    Dim view As DataView = ds.Tables(0).DefaultView
    source1.DataSource = view
    DataGridView1.DataSource = view
Else
    MsgBox("NO DATA!")
End If


这篇关于如何使用vb.net中的数据库中的文本框搜索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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