简体   繁体   English

仅使用 2 个文本框使用 VB.net 搜索数据库 MS Access

[英]Searching database MS Access with VB.net with 2 textbox only

I do the simple project using VB.net 2008 to link with MS Access 2007. I have two textbox which is data for ID and Fullname.我使用 VB.net 2008 与 MS Access 2007 链接进行了简单的项目。我有两个文本框,它们是 ID 和全名的数据。

txtID: ID and txtFullname : Fullname and Button1 : Search txtID:ID 和 txtFullname:全名和 Button1:搜索

In MS Access My data for ID : 1,2 & 3 and My data for Fullname : John, Peter & Mike在 MS Access My data for ID : 1,2 & 3 和 My data for Fullname : John, Peter & Mike

The problem I facing now is only when I key-in data '1' in txtID, output was appear in txtFullname.我现在面临的问题是,只有当我在 txtID 中键入数据“1”时,输出才会出现在 txtFullname 中。 But when I type '2' in txtID, the output is not appear in txtFullname.但是当我在 txtID 中键入“2”时,输出不会出现在 txtFullname 中。

Only ID 1 I can call.我只能呼叫 ID 1。 BUt another ID can't.但是另一个 ID 不能。

PLease Help me.请帮我。

This is my code :这是我的代码:

Imports System.Data.OleDb

Public Class Form1公开课表1

Dim con As New OleDbConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\sample.accdb"
    con.Open()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim SQL As String = "SELECT * FROM phonebookTable WHERE ID= ?"

    If con.State = ConnectionState.Closed Then
        con.Open()
    End If

    Using cmd As New OleDbCommand(SQL, con)
        cmd.Parameters.AddWithValue("@p1", Convert.ToInt32(txtID.Text))

        Dim rdr As OleDbDataReader = cmd.ExecuteReader
        If rdr.HasRows Then
            rdr.Read()
            ' use the actual DB column name
            txtFullname.Text = rdr.Item("Fullname").ToString
        Else
            txtFullname.Text = "No records"
        End If
    End Using
End Sub

End Class结束类

 da = New OleDbDataAdapter("SELECT * FROM phonebookTable where ID = '" & txtID.text & "'", con)

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

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