簡體   English   中英

如何使用ASP.NET在Access數據庫中插入關系數據

[英]How to insert relational data in Access database using ASP.NET

我有兩個關系表: Profiles表包含3種用戶角色(經理,開發人員,普通用戶),而Users表包含有關Access 2010數據庫中用戶及其角色ID(Profile_ID字段)的信息。

我在ASP.NET創建了一個Webform,該Webform應該簡單地注冊用戶,詢問用戶名,在下拉列表中選擇其角色並將其全部插入Access數據庫中。 如以下代碼所示:

Dim cs As String = ConfigurationManager.ConnectionStrings("Access 2010").ConnectionString
    Dim cn As New OleDbConnection(cs)
    Dim cmd As New OleDbCommand
    With cmd
        .CommandText = "INSERT INTO Users (nome, Profile_ID) VALUES ('" & Me.txtNome.Text & "', " & Me.ddRoles.SelectedIndex & ")"
        .Connection = cn
        .Connection.Open()
        .ExecuteNonQuery()
        .Connection.Close()
        .Dispose()
    End With
    cn.Dispose()

碰巧我無法插入數據庫,因為它們彼此之間存在關系,這給了我一個錯誤。 實際上,我只需要在Users表中插入數據,因為角色是固定的。 我該怎么做?

編輯

我得到的錯誤是You can not add or change records, it is necessary that they have a related record in table 'Profiles'.

更改命令文本,以使用下拉列表的SelectedValue而不是所選索引。

.CommandText = "INSERT INTO Users (nome, Profile_ID) VALUES ('" & Me.txtNome.Text & "', " & Me.ddRoles.SelectedValue & ")"

如果這樣不起作用,則需要確保下拉列表已正確綁定到角色名稱/ ID。

插入表名(papercode,paper type,semster,dato)中的值('“&textbox1.text&”','“&dropdownlist1.text&”')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM