繁体   English   中英

尝试使用外键从组合框中获取数据并更新数据库

[英]Trying to take data from a combo box with a foreign key and update the database

I am trying to update my SQL database with the code provided, I am basically trying to take a select statement to bring back just the INTGenderID based on a combo box and make it equal to a string then convert that to an integer when I do my更新回我的数据库。 如果您需要更多信息,请告诉我。 主表是 TGolfers,父表是 TGenders。

我想我理解这个问题。 假设您有一个名为Thing的数据库表,其中包含ThingIdName列。 您通常会查询数据库以填充DataTable ,然后将其绑定到您的ComboBox ,例如

Dim table As New DataTable

Using connection As New OleDbConnection(connectionString),
      command As New OleDbCommand("SELECT ThingId, Name FROM Thing", connection)
    connection.Open()

    Using reader = command.ExecuteReader()
        table.Load(reader)
    End Using
End Using

With thingComboBox
    .DisplayMember = "Name"
    .ValueMember = "ThingId"
    .DataSource = table
End With

用户现在将在ComboBox中看到Name值列表,当它们为 select 时,您可以从SelectedValue属性中获取相应的ThingId ,例如

Dim thingId = CInt(thingComboBox.SelectedValue)

暂无
暂无

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

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