C# 如何将多个值输入到具有复合表的数据库表中?

C# 如何将多个值输入到具有复合表的数据库表中?,c#,mysql,sql,database,C#,Mysql,Sql,Database,嘿,伙计们,我在将我的调查数据插入到一个数据库时遇到了一些问题,该数据库有一堆具有外键+主键关系的复合表。你可以从上面的ERDiagram中看到这种关系,我知道我把M:M关系搞砸了,但是如果你可以忽略这一点,你可以从PK和FK中看到这种关系 以下是我在WebFormApplication中的C代码: int GID, AID, SID, ISP =0; string homeSub, homePC, workSub, workPC, email =""; List

嘿,伙计们,我在将我的调查数据插入到一个数据库时遇到了一些问题,该数据库有一堆具有外键+主键关系的复合表。你可以从上面的ERDiagram中看到这种关系,我知道我把M:M关系搞砸了,但是如果你可以忽略这一点,你可以从PK和FK中看到这种关系

以下是我在WebFormApplication中的C代码:

int GID, AID, SID, ISP =0;
        string homeSub, homePC, workSub, workPC, email ="";
        List<int> CarMakes = new List<int>();
        List<int> Banks = new List<int>();
        List<int> addService = new List<int>();
        List<int> addISP = new List<int>();

        GID = rblGender.SelectedIndex+1;
        AID = ddlAge.SelectedIndex+1;
        SID = ddlState.SelectedIndex + 1;
        ISP = ddlISP.SelectedIndex + 1;

        homeSub = tbxHomeSuburb.Text;
        homePC = tbxHomePostCode.Text;
        workSub = tbxWorkSuburb.Text;
        workPC = tbxWorkPostcode.Text;
        email = tbxEmailAdd.Text;

        //Validation stuff here:

        //if empty blah blah

        SqlConnection myConnection;
        SqlCommand queryCommand;
//This string is correct, I have used it plenty of times to Databind data to the form            
String myConnectionString = "Login Creditials are hidden";

        myConnection = new SqlConnection();
        myConnection.ConnectionString = myConnectionString;
        //I need one command to insert all survey data into their respective tables with the RID as a FK to connect all the data back to that respondent
        String sqlCommand = "INSERT INTO RESPONDENT (email, home_postcode, home_suburb, work_postcode, work_suburb) VALUES ('"+email+"','"+homePC+"','"+homeSub+"','"+workPC+"','"+workSub+"');";

        queryCommand = new SqlCommand(sqlCommand, myConnection);

        // Perform connect
        myConnection.Open();
        SqlDataReader dataReader = queryCommand.ExecuteReader();
如果按钮单击事件中的信息有用,则会触发此操作。如果有人能帮助解决这个问题,请提前感谢

您将插入到响应表中。 获取创建的自动数字RID 使用RID执行muliples Insert逐个插入到另一个表中。
你得到的错误是什么?没有特别的错误。我只是想知道如何将我从调查中收集的所有数据一次插入到单独的表中。从每个父子关系的父表开始,一次插入一个表中的数据。好的,现在我似乎遇到了一个错误:System.data.SqlClient.SqlException类型的异常发生在System.data.dll中,但未在用户代码中处理其他信息:insert语句与外键约束FK_SEQUENCE_Respond冲突。数据库DB_9AB8B7_DDA5041表dbo.GENDER列“GID”中发生冲突。对于子表中的外键,相应的主键必须预先存在于父表中。啊,自动数字RID就是我卡住的地方。因此,在应答表上使用SCOPE_IDENTITY将返回先前自动递增的RID?是的,但您确实在一个SqlCommand中插入和选择,如示例所示