本文介绍了如何从访问数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用VB 2015从我的访问数据库中检索数据,所有数据都能够在运行时在表单中的数据网格视图中显示,但是我使用COMBO BOXES AND PICTURE BOX从表单中收集的数据无法显示在表格中的组合框和图片框中,我可以立即编辑或删除它。 请帮助我, 提前感谢。 我尝试了什么:
I try retrieve data from my access data base using VB 2015, all the data where able to displayed on data grid view in the form when run, but the data i collect from the form using COMBO BOXES AND PICTURE BOX could not displayed on the combo box and picture box in the form so that i could be able to edit it or delete it using the form instantly. please help me, thanks in advance. What I have tried:
Private Sub ButtonSEARCH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSEARCH.Click If TextBoxSEARCH.Text = "" Then MsgBox("Please check, the SEARCH space is blank", vbExclamation) Else myConnection.Open() Dim str As String str = "SELECT * FROM harry WHERE (Yname ='" & TextBoxSEARCH.Text & "')" Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection) dr = cmd.ExecuteReader If dr.Read Then TextBoxNAME.Text = dr("Yname").ToString TextBoxPHONE.Text = dr("Phone").ToString TextBoxEMAIL.Text = dr("Email").ToString TextBoxADDRESS.Text = dr("Address").ToString TextBoxDURATION.Text = dr("Duration").ToString TextBoxHOMEcongr.Text = dr("Homecongr").ToString TextBoxSPOUSEphone.Text = dr("Spousephone").ToString ComboBoxGENDER.Text = dr("Gender").ToString ComboBoxMARITALstatus.Text = dr("Maritalstatus").ToString ComboBoxVISITOR.Text = dr("Visitor").ToString Dim conn As New OleDb.OleDbConnection Dim reader As OleDb.OleDbDataReader conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DIRECTORY.accdb" conn.Open() cmd = conn.CreateCommand() cmd.CommandText = "SELECT * FROM harry WHERE (Yname ='" & TextBoxSEARCH.Text & "')" reader = cmd.ExecuteReader If reader.Read Then Dim ms As New MemoryStream() Dim arrimage() As Byte If (PictureBoxMAIN.Image IsNot Nothing) Then PictureBoxMAIN.Image.GetType() arrimage = ms.GetBuffer ms.Close() End If dr.Close() End If reader.Close() conn.Close() cmd.Dispose() conn.Dispose() End If myConnection.Close() TextBoxSEARCH.Text = "" End If End Sub<pre>推荐答案
永远不要通过连接用户输入来构建SQL查询,它被命名为SQL注入,它对您的数据库很危险并且容易出错。 名称和程序中的单引号崩溃。 SQL注入 - 维基百科 [ ^ ] SQL注入 [ ^ ] Never build an SQL query by concatenating with user inputs, it is named "SQL injection", it is dangerous for your database and error prone. A single quote in a name and your program crash. SQL injection - Wikipedia[^] SQL Injection[^]
更多推荐
如何从访问数据库中检索数据
发布评论