简体   繁体   English

VB.NET 2010数据库搜索

[英]VB.NET 2010 database search

I have a ms access database connected to my vb application through the data wizard. 我有一个通过数据向导连接到我的vb应用程序的ms Access数据库。 i want to allow the user to search the database and display their results on a datagrid. 我想允许用户搜索数据库并在数据网格上显示其结果。 for example user searches for 50 – 55 year Old man under 1.8 meters in height 例如,用户搜索身高在1.8米以下的50 – 55岁的老人

so far i can display the total amount of people on the database with this code 到目前为止,我可以使用此代码显示数据库上的总人数

    Private Sub lblTotalPeople_Click(sender As System.Object, e As System.EventArgs) Handles lblTotalPeople.Click
    Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\AssignmentDatabase.accdb")
    ' Use wildcard'
    Dim cmd As OleDbCommand = New OleDbCommand("Select COUNT(*) From Table1", con)
    '' or Where username='" & TextBox1.Text & "'
    con.Open()
    Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
    Dim myDataSet As DataSet = New DataSet()
    myDA.Fill(myDataSet, "Table1")
    DataGridView2.DataSource = myDataSet.Tables("Table1").DefaultView

End Sub

how would i search the database based on what the user searches or what would i use? 我将如何根据用户搜索的内容或使用的内容搜索数据库?

Assuming you don't already you need to learn how to use SQL. 假设您还不需要学习如何使用SQL。

In your code above the SQL statement is 在您的代码上方,SQL语句为

Select COUNT(*) From Table1

You would need to replace this SQL with a search that uses a value from the user (most likely from a textbox). 您将需要使用使用来自用户(最有可能来自文本框)的值的搜索来替换此SQL。 This text 本文

'' or Where username='" & TextBox1.Text & "'

appears to be part way to some SQL that may work but it looks dangerous. 似乎是某些SQL可行的一部分,但看起来很危险。 You should also research SQL Injection as using this directly means users could access/damage your Access database. 您还应该研究SQL注入,因为直接使用SQL注入意味着用户可以访问/损坏您的Access数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM