搜索访问数据库并显示在网格视图VB.NET中找到的所有数据 [英] Search access DB and display all data found in grid view VB.NET
问题描述
我希望在VB NET中创建一个用户窗体,该窗体具有一个文本框,我可以将其放入"AAA00 AA000
"格式的字符串中(这是我的第二列)TO
,然后使用按钮进行搜索访问数据库并返回与该编号有关的所有数据,并以相同的形式显示在gridview中.
我的标题将由我的数据库的第一行定义
任何人都可以向我指出正确的方向,香港专业教育学院用Excel在vb中进行了一些编程,但这是我通过Visual Studio在vb中进行的第一个项目.
谢谢izzy
我尝试过的事情:
I wish to create a userform in VB NET, that has a single textbox that I can put in a string in the format "AAA00 AA000
" (this is in my second column) TO
then use a button to search access database and return all data relating to that number and display in gridview on the same form.
my headings will be defined by the first row of my DB
Can anyone point me in the right direction, ive done some programming in vb with excel but this is my first project in vb via visual studio
Thanks izzy
What I have tried:
Imports System.Data.OleDb
Public Class Form1
'Change "C:\Users\Jimmy\Documents\Merchandise.accdb" to your database location
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\IzzyM\Desktop\Engie\KKS\KKS Maintenance Tool.accdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
Dim provider As String
Dim dataFile As String
Public myConnection As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader
'Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'MyConn = New OleDbConnection
'MyConn.ConnectionString = connString
'ds = New DataSet
'tables = ds.Tables
'da = New OleDbDataAdapter("Select * from [EWH INSTRUMENT KKS]", MyConn) 'Change items to your database name
'da.Fill(ds, "items") 'Change items to your database name
'Dim view As New DataView(tables(0))
'source1.DataSource = view
'DataGridView1.DataSource = view
'End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim connstring As String
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "C:\Users\IzzyM\Desktop\Engie\KKS\KKS Maintenance Tool.accdb" ' Change it to your Access Database location
connString = provider & dataFile
myConnection.ConnectionString = connString
End Sub
Private Sub Button1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
myConnection.Open()
DescriptionText.Clear()
CostText.Clear()
PriceText.Clear()
Dim str As String
str = "SELECT * FROM Items WHERE (Code = '" & CodeText.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
DescriptionText.Text = dr("Description").ToString
CostText.Text = dr("Cost").ToString
PriceText.Text = dr("Price").ToString
End While
myConnection.Close()
End Sub
End Class
推荐答案
首先,切勿使用串联字符串作为查询.这使您可以使用 SQL注入 [参数化查询 [ OleDbCommand.Parameters属性(系统. Data.OleDb) [ ^ ]
处理数据的正确方法是创建数据访问层 [商务逻辑层 [编写便携式数据访问层 [ ^ ]和以下内容: ^ ]
First of all, NEVER use concatenating string as a query. This exposes you on Sql Injection[^]. Rather than this, use parameterized queries[^].
See: OleDbCommand.Parameters Property (System.Data.OleDb)[^]
A proper way to work with data is to create Data Access Layer[^] and Bussines Logic Layer[^].
Finally, i''d strongly recommend to read this: Writing a Portable Data Access Layer[^] and this: Simplified Database Access via ADO.NET Interfaces[^]
这篇关于搜索访问数据库并显示在网格视图VB.NET中找到的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!