OleDb读取数据库并使用组合框填充文本框

时间:2014-06-22 16:18:18

标签: vb.net combobox textbox oledb

好的,我已将代码调整为我在此推荐的内容,但只有TxtNaamprev.Text和TxtAdresprev.Text才会填写... 有人可以填写缺失的链接吗?我真的不知道我在做什么。这是我们的书解释之外的学校任务;我没有参考,但互联网。

Private Sub CbbNaamfirma_SelectedIndexChanged(sender As System.Object, e As                                               System.EventArgs) Handles CbbNaamfirma.SelectedIndexChanged
    Dim cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
                Application.StartupPath & "\Database.accdb"
    Dim query = "SELECT Adres FROM TblKlanten WHERE [Naam firma] = ?"
    Using Connection = New OleDb.OleDbConnection(cnString)
        Using cmd = New OleDbCommand(query, Connection)
            Try
                Connection.Open()
                cmd.Parameters.AddWithValue("@p1", CbbNaamfirma.Text)
                Using Reader = cmd.ExecuteReader
                    While Reader.Read
                        TxtNaamprev.Text = CbbNaamfirma.Text
                        Dim posAdres = Reader.GetOrdinal("Adres")
                        TxtAdresprev.Text = Reader.GetString(posAdres)

                        Dim posTelefoonnummer = Reader.GetOrdinal("Telefoonnummer")
                        TxtTelefoonprev.Text = Reader.GetInt16(posTelefoonnummer)

                        Dim posFaxnummer = Reader.GetOrdinal("Faxnummer")
                        TxtFaxprev.Text = Reader.GetString(posFaxnummer)

                        Dim posGSM = Reader.GetOrdinal("GSM")
                        TxtGsmprev.Text = Reader.GetString(posGSM)

                        Dim posBTW = Reader.GetOrdinal("BTW-nummer")
                        TxtBTWprev.Text = Reader.GetString(posBTW)

                        Dim posWebsite = Reader.GetOrdinal("Website")
                        TxtWebsiteprev.Text = Reader.GetString(posWebsite)

                        Dim posNaambank = Reader.GetOrdinal("Naam bank")
                        TxtBankprev.Text = Reader.GetString(posNaambank)

                        Dim posIBAN = Reader.GetOrdinal("IBAN-nummer")
                        TxtIBANprev.Text = Reader.GetString(posIBAN)

                        Dim posBIC = Reader.GetOrdinal("BIC-code")
                        TxtBICprev.Text = Reader.GetString(posBIC)
                    End While
                End Using
            Catch ex As OleDbException
                MessageBox.Show(ex.Message)
            End Try
        End Using
    End Using
End Sub

1 个答案:

答案 0 :(得分:0)

正如Plutonix所指出的,您的SELECT查询只选择一列(Adres)。正在返回您想要的其他列,因为查询没有要求它们。您可以使用*获取与WHERE条件匹配的记录的所有列,也可以列出所需的每个列,将每个列名称分隔,,如下所示:< / p>

Dim query = "SELECT Adres, Telefoonnummer, Faxnummer, GSM, BTW-nummer, Website, [Naam bank], IBAN-nummer, BIC-code FROM TblKlanten WHERE [Naam firma] = ?"

或者

Dim query = "SELECT * FROM TblKlanten WHERE [Naam firma] = ?"

另外,正如Plutonix建议的那样,您可以使用列名来获取列的值。由于您要将值分配给TextBoxes,因此您需要字符串:

TxtAdresprev.Text = Reader("Adres").ToString()
TxtTelefoonprev.Text = Reader("Telefoonnumer").ToString()
' And so on.....

当最终目的地期望Object时,无需从String转换为基础数据类型。