使用C#将数据同时导出到Access中的两个表中

使用C#将数据同时导出到Access中的两个表中,c#,sql,ms-access,insert,C#,Sql,Ms Access,Insert,我试图在一个表中输入id、名字和姓氏,然后根据组合框输入我创建另一个记录,然后保存学生id和在组合框中选择的团队id。这是我的密码。一切运行良好唯一的问题是,在此之后,TeamPlayers表中的记录没有添加。拜托谁 try { string team = null; using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Sou

我试图在一个表中输入id、名字和姓氏,然后根据组合框输入我创建另一个记录,然后保存学生id和在组合框中选择的团队id。这是我的密码。一切运行良好唯一的问题是,在此之后,TeamPlayers表中的记录没有添加。拜托谁

try
   {
      string team = null;

            using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=;Persist Security Info=False;"))
            {
                OleDbCommand comm = new OleDbCommand("INSERT INTO Students(BaruchID, FirstName, LastName) VALUES(@id, @first, @last)", conn);
                conn.Open();
                comm.Parameters.AddWithValue("@id", tbBaruchID.Text);
                comm.Parameters.AddWithValue("@id", FirstName.Text);
                comm.Parameters.AddWithValue("@id", LastName.Text);
                comm.ExecuteNonQuery();
                conn.Close();
            }
                using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Junglists/Documents/Visual Studio 2013/Projects/SACC_Baruch/SACC_Baruch/Teams.accdb;Persist Security Info=False;"))
            {
                OleDbCommand comm = new OleDbCommand("SELECT TeamNum FROM Teams WHERE TeamName='" + cbTeam.Text +"'", conn);
                conn.Open();
                OleDbDataReader dr = comm.ExecuteReader(CommandBehavior.SingleResult);

                if (dr.Read())
                {
                    team = dr["TeamNum"].ToString();
                }
                conn.Close();
             }
                using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Junglists/Documents/Visual Studio 2013/Projects/SACC_Baruch/SACC_Baruch/Teams.accdb;Persist Security Info=False;"))
            {
                OleDbCommand comm = new OleDbCommand("INSERT INTO TeamPlayers(ID, BaruchID, TeamID) VALUES(@i, @id, @teamid)", conn);
                conn.Open();
                comm.Parameters.AddWithValue("@i", 1);
                comm.Parameters.AddWithValue("@id", tbBaruchID.Text);
                comm.Parameters.AddWithValue("@teamid", int.Parse(team));
                conn.Close();
            }

            MessageBox.Show("Student Added for team"+ cbTeam.Text);
        }

添加参数值时,不使用第一条INSERT语句中的参数名称。所有
comm.Parameters.AddWithValue(“@id”,…)行使用
@id
。因此,实际的id值永远不会保存到student表中

第二次插入始终使用1作为“id”的值。假设“id”是主键,它只能包含唯一的值。将“id”字段设为标识字段,然后将其从INSERT语句中删除。每次将一条记录添加到表中时,都会以递增的顺序给它下一个数字


更正代码:

添加参数值时,不使用第一条INSERT语句中的参数名称。所有
comm.Parameters.AddWithValue(“@id”,…)行使用
@id
。因此,实际的id值永远不会保存到student表中

第二次插入始终使用1作为“id”的值。假设“id”是主键,它只能包含唯一的值。将“id”字段设为标识字段,然后将其从INSERT语句中删除。每次将一条记录添加到表中时,都会以递增的顺序给它下一个数字

更正代码: