简体   繁体   English

如何使用ASP.NET在Access数据库中插入关系数据

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

I have two relational tables: the Profiles table which contains 3 kinds of user roles (Manager, Developer, Common User) and the Users table which contains information about the user and their roles ID (Profile_ID field) in the Access 2010 database. 我有两个关系表: Profiles表包含3种用户角色(经理,开发人员,普通用户),而Users表包含有关Access 2010数据库中用户及其角色ID(Profile_ID字段)的信息。

I created a Webform in ASP.NET which should simply register users, asking for their names, selecting their roles in a dropdown list and inserting it all in the Access database. 我在ASP.NET创建了一个Webform,该Webform应该简单地注册用户,询问用户名,在下拉列表中选择其角色并将其全部插入Access数据库中。 As in the following code: 如以下代码所示:

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()

It happens that I can't insert in the database because they have a relationship between each other, it gives me an error. 碰巧我无法插入数据库,因为它们彼此之间存在关系,这给了我一个错误。 Actually, I just need to insert data in the Users table 'cause the roles are fixed. 实际上,我只需要在Users表中插入数据,因为角色是固定的。 How can I do it? 我该怎么做?

EDIT 编辑

The error I get is You can not add or change records, it is necessary that they have a related record in table 'Profiles'. 我得到的错误是You can not add or change records, it is necessary that they have a related record in table 'Profiles'.

Change your command text to take the SelectedValue of the dropdown instead of the selected index. 更改命令文本,以使用下拉列表的SelectedValue而不是所选索引。

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

If that doesn't work you need to make sure that the drop down is being bound properly to the Role name/id. 如果这样不起作用,则需要确保下拉列表已正确绑定到角色名称/ ID。

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

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

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