Access Primary Key usage/role

人走茶凉 提交于 2019-12-02 18:03:19

问题


My question is in relation to connecting a VB.net project to an Access DB. Must I have a primary key in my table, or have is there a way to alter my code to not look for a PK?

I have an error popping up telling me that I have no primary key. Now if i make "AdminID" my primary key, the system works. I was just questioning as to whether or not this is required?

Currently my combobox displays "AdminID" where as I would rather it display "AdminName"

Thanks.

Dim objConnection As New   OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= InfoSystem.accdb")
Dim objAdminDA As New OleDb.OleDbDataAdapter("Select *from tblAdmins", objConnection)
Dim objAdminCB As New OleDb.OleDbCommandBuilder(objAdminDA)
Dim objDataSet As New DataSet()

Public Sub Retrieve()

    'Clears DataSet of any existing data
    objDataSet.Clear()
    'Fills schema - adds table structure information to DataSet
    objAdminDA.FillSchema(objDataSet, SchemaType.Source, "tblAdmins")
    'Fills DataSet with info from the DataAdapter
    objAdminDA.Fill(objDataSet, "tblAdmins")

    'Fill the DataSet with info from the Admin table
    objAdminDA.FillSchema(objDataSet, SchemaType.Source, "tblAdmins")
    objAdminDA.Fill(objDataSet, "tblAdmins")

    'Empty combo box
    cboxAdmin.Items.Clear()

    'Loop through each row, adding the AdminName to the combo box
    Dim i As Integer, strAdminID As String
    For i = 1 To objDataSet.Tables("tblAdmins").Rows.Count
        strAdminID = objDataSet.Tables("tblAdmins").Rows(i - 1).Item("AdminID")
        cboxAdmin.Items.Add(strAdminID)
    Next
    'Select first item in the list
    cboxAdmin.SelectedIndex = 0

    FillAdminDetails()

End Sub

Public Sub FillAdminDetails()
    Dim objRow As DataRow
    objRow = objDataSet.Tables("tblAdmins").Rows.Find(cboxAdmin.SelectedItem.ToString)
    txtStaffDept.Text = objRow.Item("Department")
    txtStaffTitle.Text = objRow.Item("Title")
End Sub

Private Sub cboxAdmin_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboxAdmin.SelectedIndexChanged
    FillAdminDetails()
End Sub

End Sub

回答1:


Your CBO shows AdminID because that is what you fill it with. If you bind the table to the CBO, you can use DisplayMember to show one thing, and ValueMember to return another to your code:

Private ACEConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= InfoSystem.accdb"
Private dtAdmin As DataTable
...
Private Sub SetupCBO
    Dim SQL = "SELECT AdminId, AdminName FROM tblAdmin"

    Using dbcon As New OleDbConnection(ACEConnStr)
        Using cmd As New OleDbCommand(SQL, dbcon)

            dbcon.Open()
            dtAdmin = New DataTable

            dtAdmin.Load(cmd.ExecuteReader())
        End Using
    End Using

    cboAdmin.DataSource = dtAdmin
    cboAdmin.DisplayMember = "AdminName"     ' what to show
    cboAdmin.ValueMember = "AdminId"         ' what field to report
End Sub

The first thing to note is that there is no code to populate the CBO directly. Using a DataSource, the cbo will get the data from the underlying table. The DisplayMember tells it which column to...well, display, and the ValueMember allows your code to be able to fetch that PrimaryKey/Id to positively know which "Steve" or "Bob" is logging in.

Also note that you don't absolutely need a DataAdapter just to fill one table. Nor do you need DataSet. As shown, you can use the Load method with a DataReader to directly fill a table. The Using blocks close and dispose of those objects freeing resources.

When bound, you would generally use the SelectedValueChanged event and use the .SelectedValue.

In this case, the SelectedItem will be a DataRowView object because NET creates a DataView wrapper. If the data source was a List(Of Employee) or a List(of Widget), the SelectedItem will be the selected Employee or Widget.



来源:https://stackoverflow.com/questions/36142596/access-primary-key-usage-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!