向数据库插入新记录覆盖现有记录C#

问题描述:

我设计了一个学生调查来评估教师。该调查由20个问题组成。我想让每个学生都登录并记录他们对数据库的回答。然而,我能够设计整个事情,每次我参加一项调查时,新纪录会覆盖现有记录。我希望所有答案都能保存在每一位学生的一行中。向数据库插入新记录覆盖现有记录C#

我真的处于时间压力之下,对此的任何帮助将非常感激。

这里是Q1页面

if (Session["USER_ID"] != null) 
       { 
       SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=Student;Integrated Security=True"); 
      SqlCommand cmd = new SqlCommand("insert into Survey (Q1,Q1_Comments) values (@Q1,@Q1_Comments)", con); 
      cmd.Parameters.AddWithValue("Q1", radListQ1.SelectedValue); 
      cmd.Parameters.AddWithValue("Q1_Comments", txtQ1Comments.Text); 

      con.Open(); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
      Response.Redirect("Q2.aspx"); 
     }  

上的代码,这里是在Q2的问题

Protected void btnQ2Next_Click(object sender, EventArgs e) 
    { 
     if (Session["USER_ID"] != null) 
     { 
      SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=Student;Integrated Security=True"); 

      SqlCommand cmd = new SqlCommand("UPDATE Survey SET Q2 = @Q2, Q2_Comments = @Q2_Comments ", con); 
      cmd.Parameters.AddWithValue("Q2", radListQ2.SelectedValue); 
      cmd.Parameters.AddWithValue("Q2_Comments", txtQ2Comments.Text); 

      con.Open(); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 

      Response.Redirect("Q3.aspx"); 
     } 
    } 

请帮帮忙,我怀疑插入/更新语句的代码是什么造成的。

我也想说,每个问题都在一个单独的页面,这就是为什么我插入更新。

编辑

目前,我的数据库结构是这样的

调查表 Survey_ID(PK) 用户名(FK) student_id数据(FK) COURSE_ID(FK) Q1 Q1_评论 Q2 Q2_评论 Q3 Q3_评论 Q4 Q4_评论 ......到Q20

学生表 student_id数据(PK) 姓氏 将First_Name 用户名 密码

Course_student表 student_id数据(PK) COURSE_ID(PK) Instructor_ID(PK) Survey_ID(PK )

课程表 COURSE_ID Class_Title Instructor_Last Instructor_First 期限 SECTION_ID 课程编号

表指导员 Instructor_ID(PK) 姓氏 将First_Name 用户名 密码

登录页面代码

protected void btnLogin_Click(object sender, EventArgs e) 
    { 
     SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Student;Integrated Security=True"); 
     con.Open(); 

     SqlCommand cmd = new SqlCommand("select s.Student_ID, c.Course_ID,s.First_Name, s.Last_Name, c.Class_Title, c.Course_ID, c.Instructor_Last, c.Instructor_First, c.Term, c.Section_ID, c.Course_Number,e.Instructor_ID from Student S Join Course_Student e ON (s.Student_ID = e.Student_ID) Join Course c ON(c.Course_ID = e.Course_ID) where UserName [email protected] and [email protected]", con); 
     //SqlCommand cmd = new SqlCommand("select * from UserTable where UserName [email protected] and [email protected]", con); 
     cmd.Parameters.AddWithValue("@username", txtUserName.Text); 
     cmd.Parameters.AddWithValue("@password", txtPassword.Text); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     if (dt.Rows.Count > 0) 
     { 
      Session["USER_ID"] = dt; 
      Response.Redirect("Successful_Login.aspx"); 
     }  

Successful_Login.aspx

public partial class Successful_Login : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     DataTable dt = new DataTable(); 
     dt = (DataTable)Session["USER_ID"]; 
     lblName.Text = dt.Rows[0][0].ToString() + " " + dt.Rows[0][1].ToString();//your cloumn name; 

     DataTable dt2 = (DataTable)Session["USER_ID"]; 
     GridView1.DataSource = dt2; 
     GridView1.DataBind(); 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     Session.Remove("USER_ID"); 
     Session.RemoveAll(); 
     Session["USER_ID"] = null; 
     Response.Redirect("Loggedout.aspx"); 
    } 

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     string instructorName = GridView1.SelectedRow.Cells[5].Text + ", " + GridView1.SelectedRow.Cells[4].Text; 
     string courseSession= GridView1.SelectedRow.Cells[1].Text + "-" + GridView1.SelectedRow.Cells[2].Text; 
     string term = GridView1.SelectedRow.Cells[8].Text; 
     string studentID = GridView1.SelectedRow.Cells[10].Text; 
     string CourseID = GridView1.SelectedRow.Cells[11].Text; 

     Session["USER_ID1"] = instructorName; 
     Session["USER_ID2"] = courseSession; 
     Session["USER_ID3"] = term; 
     Session["USER_ID4"] = studentID; 
     Session["USER_ID5"] = CourseID; 



     Response.Redirect("Q1.aspx"); 
    } 

Q1页

if (Session["USER_ID"] != null) 
      { 
      SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=Student;Integrated Security=True"); 
     SqlCommand cmd = new SqlCommand("insert into Survey (Q1,Q1_Comments) values (@Q1,@Q1_Comments)", con); 
     cmd.Parameters.AddWithValue("Q1", radListQ1.SelectedValue); 
     cmd.Parameters.AddWithValue("Q1_Comments", txtQ1Comments.Text); 

     con.Open(); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     Response.Redirect("Q2.aspx"); 
    }  

Q2页

if (Session["USER_ID"] != null) 
      { 
      SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=Student;Integrated Security=True"); 
     SqlCommand cmd = new SqlCommand("insert into Survey (Q1,Q1_Comments) values (@Q1,@Q1_Comments)", con); 
     cmd.Parameters.AddWithValue("Q1", radListQ1.SelectedValue); 
     cmd.Parameters.AddWithValue("Q1_Comments", txtQ1Comments.Text); 

     con.Open(); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     Response.Redirect("Q2.aspx"); 
    }  

你能告诉我如何使用我的变量名正确写入。

+0

是什么的调查模式是什么样子?你的调查表有哪些其他领域? – Hammerstein

+0

您在第二季度没有Where语句。所以它不知道你想更新哪个记录。如果你想在不同的行中,你需要插入 – Dylan

+0

你会怎么写where子句? –

虽然它可能不会直接回答你的问题有可能导致你更好地组织你的数据库,在我眼里,你将需要改变你的数据库设计,以适应多用户,并与相应的答案多的问题,以便您的表应该是这个样子这

Table Users 
----------- 
UserId (PK) 
FirstName 
LastName 
... 

Table Questions 
--------------- 
QuestionId (PK) 
QuestionText 
... 

Table Answers 
------------- 
AnswerId (PK, Autonumber) 
UserId  - Unique constraint 
QuestionId - Unique constraint 
Answer 
... 

这样的结构,你就可以做这样的事情,那么:

if (Session["USER_ID"] != null) 
       { 
       SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=Student;Integrated Security=True"); 
      SqlCommand cmd = new SqlCommand("insert into Answers (UserId, QuestionId, Answer) values (@p1, @p2, @p3)", con); 
      cmd.Parameters.AddWithValue("p1", Session["USER_ID"]); 
      cmd.Parameters.AddWithValue("p2", radListQ1.SelectedValue); //reference question_id field here 
      cmd.Parameters.AddWithValue("p3", txtQ1Comments.Text); // reference answer field here 

      con.Open(); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
      Response.Redirect("Survey.aspx?Q=" + NextQuestionId); 
     }