我設計了一個學生調查來評估教師。該調查由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");
}
你能告訴我如何使用我的變量名正確寫入。
是什麼的調查模式是什麼樣子?你的調查表有哪些其他領域? – Hammerstein
您在第二季度沒有Where語句。所以它不知道你想更新哪個記錄。如果你想在不同的行中,你需要插入 – Dylan
你會怎麼寫where子句? –