【问题标题】:Fill Combobox with combined columns access database使用组合列访问数据库填充组合框
【发布时间】:2018-03-25 02:34:24
【问题描述】:

如何从我的 access 数据库 "contacts.accdb" 填充组合框中组合 "Firstname" "Lastname" 列?我已经将dataset +datagridview 加载到项目中的另一个表单中..

【问题讨论】:

    标签: vb.net ms-access combobox multiple-columns items


    【解决方案1】:

    一个简单但完整的例子:

    Dim con as New OledbConnection = connectionstringhere
    Dim cmd as New OleDbCommand("Select * from tablenamehere",con)
    Dim dr as OledbDataReader=cmd.ExecuteReader
    While dr.read
    Dim FnameAndLname as string = dr(0).ToString + " " + dr(1).ToString 'Assuming the FirstName is the 1st and the LastName is the 2nd Column of the table
    MyConboBox.Items.Add(FnameAndLname)
    End while
    

    希望这会有所帮助:)

    【讨论】:

    • 我想您可能想在名字和姓氏之间添加 & " " &。
    • @Mary,是的,添加了它,以防 OP 想要一个 SPACE
    • Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Contactos.accdb;Persist Security Info=False;") cn.Open() Dim cm As New OleDbCommand("select * from ContactosTabla", cn) Dim dr As OleDbDataReader = cm.ExecuteReader While dr.Read ComboBox3.Items.Add(dr(1).ToString + " " + dr(2).ToString) 'Assuming the FirstName is the 1st and the LastName is the 2nd Column of the table End While dr.Close() cn.Close()
    • 请将答案标记为答案,如果有帮助,请点赞
    猜你喜欢
    • 2014-07-29
    • 2012-09-11
    • 2014-05-03
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多