简体   繁体   English

如何使用vb.net从Access db提取数据并将其放入文本框?

[英]How to extract data from Access db and place it into a text box using vb.net?

Hi guys I'm trying to search an employee information using SQL from MS Access, and hoping to put the fname lname and such details in their respective textbox which correspond to a specific employee's ID number. 嗨,大家好,我正在尝试使用MS Access中的SQL搜索员工信息,并希望将fname lname和此类详细信息放在各自的文本框中,这些文本框对应于特定员工的ID号。 I have managed to make the SQL work but I don't know how to extract files from my sql statement and place it inside .text(text box), can you please guide me? 我设法使SQL正常工作,但是我不知道如何从sql语句中提取文件并将其放在.text(文本框)中,请您指导我? Thanks 谢谢

Here is my code so far: (UPDATED my code) got an error message : Additional information: ExecuteReader: Connection property has not been initialized. 到目前为止,这是我的代码:(更新了我的代码)收到错误消息:附加信息:ExecuteReader:连接属性尚未初始化。 Highlighting Reader below. 在下面突出显示读者。 How can i fix this? 我怎样才能解决这个问题? I'm trying to extract data and place it into the textbox? 我正在尝试提取数据并将其放入文本框? Thanks 谢谢

Private Sub eNumText_SelectedIndexChanged(sender As Object, e As EventArgs) Handles eNumText.SelectedIndexChanged


        Dim dbSource = "Data Source= C:\Databse\Company_db.accdb"
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= c:\Databse\Company_db.accdb"

        Dim sqlQuery As String
        Dim sqlCommand As New OleDbCommand
        Dim sqlAdapter As New OleDbDataAdapter
        Dim Table As New DataTable

        Dim empNum As String
        Dim empFname As String
        Dim empLname As String
        Dim empDept As String
        Dim empStat As String
        Dim empYears As String

        empNum = eNumText.Text
        empFname = empFnameText.Text
        empLname = empLnameText.Text
        empDept = DeptText.Text
        empStat = StatText.Text
        empYears = yearstext.Text

        sqlQuery = "SELECT * FROM tbl_empinfo WHERE EmpID like empNum"

        With sqlCommand
            .CommandText = sqlQuery
            .Connection = con
            .Parameters.AddWithValue("EmpID", empNum)
            With sqlAdapter
                .SelectCommand = sqlCommand
                .Fill(Table)
            End With
            With DataGridView1
                .DataSource = Table
            End With
        End With

        Dim path = "Data Source= C:\Databse\Company_db.accdb"
        Dim command = "SELECT * FROM tbl_empinfo WHERE EmpID like empNum"
        QueryData(path, command)


        con.Close()

    End Sub
    Private Sub QueryData(PathDb As String, command As String)
        Using connection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathDb)
            Using da As New System.Data.OleDb.OleDbCommand(command, connection)
                connection.Open()

                Dim reader = da.ExecuteReader()
                If reader.Read() Then
                    empFnameText.Text = reader("fname")
                    empLnameText.Text = reader("lname")
                End If

                connection.Close()
            End Using
        End Using
    End Sub

for this, you need only this classes: Connection, Command, Reader. 为此,您仅需要以下类:Connection,Command,Reader。 Other classes in the code in question, some are redundant and some are required but in complicated than a simple case presentation. 所讨论代码中的其他类,有些是冗余的,有些是必需的,但是比简单的案例演示要复杂。

Private Sub QueryData(PathDb As String, command As String)
    Using connection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathDb)
        Using com As New System.Data.OleDb.OleDbCommand(command, connection)
            connection.Open()

            Dim reader = com.ExecuteReader()
            If reader.Read() Then
                TextBox1.text = reader("fname")
                TextBox2.text = reader("lname")
            End If

            connection.Close()
        End Using
    End Using
End Sub

call the method so: 调用方法如下:

Dim path = "C:\Databse\Company_db.accdb"
Dim command = "SELECT * FROM tbl_empinfo WHERE EmpID like empNum"
QueryData(path, command)

EDIT - Use parameters: 编辑 -使用参数:

Private Sub QueryData(PathDb As String, command As String, id As String)
    Using connection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathDb)
        Using com As New System.Data.OleDb.OleDbCommand(command, connection)
            com.Parameters.AddWithValue("", id)

            connection.Open()

            Using reader = com.ExecuteReader()
                If reader.Read() Then
                    TextBox1.Text = reader("fname")
                    TextBox2.Text = reader("lname")
                End If
            End Using

            connection.Close()
        End Using
    End Using
End Sub

Call the method: 调用方法:

Dim path = "C:\Databse\Company_db.accdb"
Dim command = "SELECT * FROM tbl_empinfo WHERE EmpID = ?"
Dim empNum = "..."
QueryData(path, command, empNum)

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

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