将调查结果保存到数据库中
本文关键字:数据库 保存 调查 结果 | 更新日期: 2023-09-27 18:03:33
我正在做一个项目,需要我创建一个评估门户(调查),用户登录并回答20个问题及其所有评级问题(强烈不同意,不同意,同意和强烈同意)。
我使用ASP.net和c#和单选按钮列表在每个页面。
我可以使用sessions创建登录页面。
我想在一个单独的页面上显示每个问题,但是当我这样做时,我开始在将调查结果保存到数据库时遇到麻烦。每个问题的结果显示在数据库中的单独一行中。更具体地说,每个用户在数据库中将有20行数据。
我的问题是如何在数据库的同一行或一行中获得每个用户的响应??
任何帮助都将非常感激。
感谢这是我的登录页面:
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Student;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select 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 Student_Course e ON (s.Student_ID = e.Student_ID) Join Course c ON(c.Course_ID = e.Course_ID) where UserName =@username and Password=@password",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");
}
这是我成功登录的页面这个页面是为了获取用户登录信息,我使用gridview来显示学生登录的课程,从那里他们可以点击他们想要评分的课程并开始调查。
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 GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string instructorName = GridView1.SelectedRow.Cells[4].Text + ", " + GridView1.SelectedRow.Cells[5].Text;
string courseSession= GridView1.SelectedRow.Cells[7].Text + "-" + GridView1.SelectedRow.Cells[8].Text;
string term = GridView1.SelectedRow.Cells[6].Text;
Session["USER_ID"] = instructorName;
Session["USER_ID2"] = courseSession;
Session["USER_ID3"] = term;
Response.Redirect("Q1.aspx");
}
这是第一个问题(Q1)当用户点击下一步按钮进入下一个问题时,我希望所有的回答都进入数据库,他们进入下一页(Q2),我想在同一行中捕获所有的结果。
protected void btnNext_Click(object sender, EventArgs e)
{
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();
}
对于初学者,如果您希望在一行中显示来自同一用户的所有20个响应,那么您应该在数据库中构造一个具有更多列的表。例如
Survey
-----------------------------------------------------
studentID | Q1 | Q1_comments | Q2 | Q2_comments | ...
对于第一个问题Q1,您仍然可以使用上面原始的insert
语句:
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);
对于第二个问题及以后的问题,您需要执行update
而不是插入到数据库。例如
SqlCommand cmd = new SqlCommand("UPDATE Survey SET (Q2=@Q2,Q2_comment=@Q2_Comments)", con);
cmd.Parameters.AddWithValue("Q2", radListQ2.SelectedValue);
cmd.Parameters.AddWithValue("Q2_Comments", txtQ2Comments.Text);
作为一点建议,您可能希望使用transaction
来做insert
, update
或delete
,并使用try ... catch ...
检测/处理潜在错误来包围它们。来自MSDN的更多阅读
我强烈建议您存储在20行…但是如果你坚持只用一行…我建议使用UPDATE命令而不是INSERT…
只是提供完整的代码。这是Q1
protected void btnNext_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("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();
}
}
这是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");
}
}