如何在vb.net中搜索数据库中的数据 [英] How to search data from database in vb.net
本文介绍了如何在vb.net中搜索数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
先生,
我在sql server中有表名Attn,列名为Emp_Id Emp_Name和Modify。
修改数据(Admin 30/08/2015)。现在我想从这个表中搜索数据为
如果Emp_Id = Idtxt.Text和Modify = TxtDate.text(这里我只想选择日期而不是名称Admin)。
我该怎么做...........
我的原始代码如下:----------
Sir,
I have table name Attn in sql server with columns Emp_Id Emp_Name and Modify.
Modify have data like(Admin 30/08/2015). Now i want to search data from this table as
if Emp_Id=Idtxt.Text and Modify= TxtDate.text(here i want to pick only date not name Admin).
How can i do this...........
My original code below is:----------
Sub LoadData_ID_Date()
Dim cn As New SqlConnection(cnSettings2())
cn.Open()
da2 = New SqlDataAdapter("SELECT SqNo,Emp_Id,Emp_Name,Desig,Present,Modify,Verify_By from Attn where Emp_Id='" & TxtID.Text & "' and Modify = '" & Txt_Date.Text & "'", cn)
dt2 = New DataTable
da2.Fill(dt2)
sBuilder2 = New SqlCommandBuilder(da2)
dgv.Columns("SlNo").DataPropertyName = "SqNo"
dgv.Columns("id").DataPropertyName = "Emp_Id"
dgv.Columns("Nm").DataPropertyName = "Emp_Name"
dgv.Columns("Desig").DataPropertyName = "Desig"
dgv.Columns("Prsnt").DataPropertyName = "Present"
dgv.Columns("Modi").DataPropertyName = "Modify"
dgv.Columns("VerifyBy").DataPropertyName = "Verify_By"
dgv.RowsDefaultCellStyle.BackColor = Color.White
dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
dgv.AllowUserToAddRows = True
dgv.Columns(0).Visible = False
dgv.Columns(1).ReadOnly = True
dgv.Columns(2).ReadOnly = True
dgv.Columns(3).ReadOnly = True
dgv.Columns(4).ReadOnly = False
dgv.Columns(5).ReadOnly = False
dgv.Columns(6).ReadOnly = True
'list_items.Columns(0).DataPropertyName = "item_id"
dgv.DataSource = dt2
cn.Close()
TxtID.Enabled = False
Txt_Date.Enabled = False
Txt_Vby.Focus()
End Sub
推荐答案
如果我没有正确地解决你的问题,你需要改变条件。如果按名称搜索仅包含
If I undestand your question correctly, you need to change the condition. If you search by name the include only
WHERE Emp_Id=@empid
WHERE子句中的
,如果按日期搜索,则
in the WHERE clause and if you search by date then
WHERE Modify = @modifydate
示例代码与您使用的代码不同,因为您不应该直接连接值到SQL语句。这使您可以打开SQL注入。
有关更多讨论,请查看正确执行数据库操作 [ ^ ]
除了 Mika Wendelius [ ^ ],我想说:
1)你可以使用Linq [ ^ ]过滤数据集/数据表对象,
2)您的代码是 SQL注入 [ ^ ]易受伤害。
广告1)
LINQ to DataSet示例 [ ^ ]
使用DataView过滤(LINQ to DataSet) [ ^ ]
广告2)
如何:保护ASP.NET中的SQL注入 [ ^ ]
如何:防止注射ASP.NET中的攻击 [ ^ ]
存储过程是否可以防止SQL注入? [ ^ ]
SQL注入及其如何避免 [ ^ ]
动态SQ L& SQL注入 [ ^ ]
In addition to solution 1 by Mika Wendelius[^], i would say that:
1) you can use Linq[^] to filter dataset/datatable object,
2) your code is SQL Injection[^] vulnerable.
Ad 1)
LINQ to DataSet Examples[^]
Filtering with DataView (LINQ to DataSet)[^]
Ad 2)
How To: Protect From SQL Injection in ASP.NET[^]
How To: Protect From Injection Attacks in ASP.NET[^]
Do Stored Procedures Protect Against SQL Injection?[^]
SQL Injection and how to avoid it[^]
Dynamic SQL & SQL injection[^]
这篇关于如何在vb.net中搜索数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文