简体   繁体   English

VB.Net 2010 - 从MDB访问数据库填充ComboBox

[英]VB.Net 2010 - Populating a ComboBox from an MDB access database

I am currently trying to populate a drop box on a form in VB.Net from an MDB database for a project I am working on for work. 我目前正在尝试从MDB数据库填充VB.Net中表单上的一个下拉框,用于我正在工作的项目。

I have a table called "Months" in a database called "cancmov", and the field i am trying to pull from is called MoveMonth. 我在一个名为“cancmov”的数据库中有一个名为“Months”的表,我想要拉出的字段叫做MoveMonth。

The current code i am using is:- 我目前使用的代码是: -

Private Sub drpMovedFrom_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles drpMovedFrom.SelectedIndexChanged


    Dim ConnnectionString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Admin\Desktop\CancellationsAndMovements\CancellationsandMovements\cancmov.mdb"
    Dim db As String = "SELECT MoveMonth FROM Months"
    Dim cn As New OleDbConnection(ConnnectionString)
    Dim da As New OleDbDataAdapter(db, cn)
    Dim ds As New DataSet()

    da.Fill(ds, "MoveMonth")

    With drpMovedFrom
        .DataSource = ds.Tables("MoveMonth")
        .SelectedIndex = 0
    End With

End Sub

As you can probably gather from the above, I am currently in the process of learning, so a lot of the above i am trying to get my head around. 正如你可能从上面收集的那样,我目前正处于学习的过程中,所以我上面的很多内容都是为了让我了解一下。 I actually pulled the above code from a similar question that had been asked i found on google. 实际上,我从谷歌上发现的类似问题中提取了上述代码。 Any help will be greatly appreciated, even if it is just to point me at a resource where i can learn to do this for myself. 任何帮助将不胜感激,即使它只是指向我可以学习为自己这样做的资源。

Thanking you in advance. 提前感谢你。

A couple things right off the bat: 马上做几件事:

You are calling this code when the drop down index changes. 您在下拉索引更改时调用此代码。 Probably not what you want since it will always set it back to zero. 可能不是你想要的,因为它总会把它设置回零。

Also, try enclosing your disposable objects in using brackets: 另外,请尝试使用括号括起您的一次性物品:

Private ds As New DataSet()
Private ConnnectionString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Admin\Desktop\CancellationsAndMovements\CancellationsandMovements\cancmov.mdb"

Protected Overrides Sub OnLoad(e As EventArgs)
  Dim db As String = "SELECT MoveMonth FROM Months"

  Using cn As New OleDbConnection(ConnnectionString)
    Using da As New OleDbDataAdapter(db, cn)
      da.Fill(ds, "MoveMonth")
    End Using
  End Using

  With drpMovedFrom
      .DisplayMember = "MoveMonth"
      .DataSource = ds.Tables("MoveMonth")
      .SelectedIndex = 0
  End With

  MyBase.OnLoad(e)
End Sub

Also, the ComboBox has DisplayMember and ValueMember properties that you map to your DataSource. 此外,ComboBox具有您映射到DataSource的DisplayMember和ValueMember属性。 DisplayMember is the value you see visible in the list, ValueMember is typically an ID value for the visible property. DisplayMember是您在列表中看到的值,ValueMember通常是visible属性的ID值。

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

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