使用 SQL 调用从数据库检索数据后的特定 if else 语句。视觉工作室 2015/C#
Specific if else statement after using a SQL call to retrieve data from a database. Visual Studios 2015 / C#
我正在尝试为我一直在做的项目画龙点睛,目前我正在尝试修改我创建的功能。特点是如果学生完成了考试,他们就可以查看结果。但是,我想做的是创建一个 if else 语句,它本质上是:如果已经参加并完成了考试,那么他们将被重定向到显示特定考试结果的页面。否则,它 returns 页面顶部的消息说明 "This examination has not been completed yet."
我目前的代码(通过页面上的按钮操作)是:
protected void btnViewPrevExam_Click(object sender, EventArgs e)
{
Session["intExaminationID"] = ddlExamination.SelectedValue;
Int32 int32StudentID = Convert.ToInt32(Session["StudentID"]);
Session["int32StudentID"] = Convert.ToInt32(int32StudentID);
// Define the ADO.NET connection object.
SqlConnection objSqlConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["OPT"].ConnectionString);
// Develop the SQL call.
// Develop the SQL call.
String strSQL = "";
strSQL = "SELECT AnswerID, Question, OptionA, OptionB, OptionC, OptionD, CorrectAnswer, Answer ";
strSQL += " FROM Question, Answer, Examination, Student ";
strSQL += " WHERE Examination.ExaminationID = " + ddlExamination.SelectedValue;
strSQL += " AND Student.StudentID = " + int32StudentID;
strSQL += " AND Answer.QuestionID = Question.QuestionID ";
strSQL += " AND Answer.StudentID = Student.StudentID ";
strSQL += " AND Examination.ExaminationID = Question.ExaminationID ";
// Create the SQL command object.
SqlCommand objSqlCommand = new SqlCommand(strSQL, objSqlConnection);
// Retrieve the row from the table.
objSqlConnection.Open();
SqlDataReader objSqlDataReader = objSqlCommand.ExecuteReader();
objSqlDataReader.Read();
if (strSQL != null)
{
objSqlDataReader.Close();
objSqlConnection.Close();
Response.Redirect("StudentExamResults.aspx");
}
else
{
this.Master.MessageForeColor = System.Drawing.Color.Red;
this.Master.Message = "The selected examination has not been completed.";
}
}
这个按钮目前的作用是无论考试是否完成,都会将学生转到考试结果页面。这是由于行 "if (strSQL != null)" 并且它永远不会为空,因为 SQL 调用已经完成并被填充。我已经尝试过其他想法,以及为 AnswerID 执行 objSqlDataReader,但它没有正常工作。这是我想添加到这个项目中的一个小额外功能,如果我能找到一些帮助来解决我做错的事情,我将非常高兴。提前致谢!
测试 strSQL
是否不是 null
将始终成功,因为您在方法的前面将其设置为非 null
值。
要查看之前完成的检查的记录是否已经存在,您需要检查调用 objSqlDataReader.Read()
的 return 值;它会 return true
只要有额外的行(或者,在这种情况下,第一行)从您的 SELECT
查询中使用。因此,改变这个...
objSqlDataReader.Read();
if (strSQL != null)
{
...到此...
if (objSqlDataReader.Read())
{
作为附加说明,请考虑将 objSqlConnection
、objSqlCommand
和 objSqlDataReader
包装在 using
blocks 中,以确保它们正确 closed/disposed。就像现在一样,当需要完成考试时,你并没有关闭 objSqlDataReader
和 objSqlConnection
,并且根本没有处理 objSqlCommand
。 objSqlDataReader
然后将按如下方式关闭,无论采用 if
的哪个分支...
using (SqlDataReader objSqlDataReader = objSqlCommand.ExecuteReader())
{
if (objSqlDataReader.Read())
{
//objSqlDataReader.Close();// No longer necessary - handled by using
objSqlConnection.Close();
Response.Redirect("StudentExamResults.aspx");
}
else
{
this.Master.MessageForeColor = System.Drawing.Color.Red;
this.Master.Message = "The selected examination has not been completed.";
}
}
假设您不关心内容,而只想检查该行是否存在,您可以这样做:
string sql = "SELECT COUNT(AnswerID) FROM Question ........ WHERE ......";
using (var connection = CreateConnection()) {
using (var cmd = new SqlCommand(sql, connection)) {
bool exists = (int) cmd.ExecuteScalar() > 0;
if (exists) {
Response.Redirect("StudentExamResults.aspx");
} else {
// Do the other thing
}
}
}
我正在尝试为我一直在做的项目画龙点睛,目前我正在尝试修改我创建的功能。特点是如果学生完成了考试,他们就可以查看结果。但是,我想做的是创建一个 if else 语句,它本质上是:如果已经参加并完成了考试,那么他们将被重定向到显示特定考试结果的页面。否则,它 returns 页面顶部的消息说明 "This examination has not been completed yet."
我目前的代码(通过页面上的按钮操作)是:
protected void btnViewPrevExam_Click(object sender, EventArgs e)
{
Session["intExaminationID"] = ddlExamination.SelectedValue;
Int32 int32StudentID = Convert.ToInt32(Session["StudentID"]);
Session["int32StudentID"] = Convert.ToInt32(int32StudentID);
// Define the ADO.NET connection object.
SqlConnection objSqlConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["OPT"].ConnectionString);
// Develop the SQL call.
// Develop the SQL call.
String strSQL = "";
strSQL = "SELECT AnswerID, Question, OptionA, OptionB, OptionC, OptionD, CorrectAnswer, Answer ";
strSQL += " FROM Question, Answer, Examination, Student ";
strSQL += " WHERE Examination.ExaminationID = " + ddlExamination.SelectedValue;
strSQL += " AND Student.StudentID = " + int32StudentID;
strSQL += " AND Answer.QuestionID = Question.QuestionID ";
strSQL += " AND Answer.StudentID = Student.StudentID ";
strSQL += " AND Examination.ExaminationID = Question.ExaminationID ";
// Create the SQL command object.
SqlCommand objSqlCommand = new SqlCommand(strSQL, objSqlConnection);
// Retrieve the row from the table.
objSqlConnection.Open();
SqlDataReader objSqlDataReader = objSqlCommand.ExecuteReader();
objSqlDataReader.Read();
if (strSQL != null)
{
objSqlDataReader.Close();
objSqlConnection.Close();
Response.Redirect("StudentExamResults.aspx");
}
else
{
this.Master.MessageForeColor = System.Drawing.Color.Red;
this.Master.Message = "The selected examination has not been completed.";
}
}
这个按钮目前的作用是无论考试是否完成,都会将学生转到考试结果页面。这是由于行 "if (strSQL != null)" 并且它永远不会为空,因为 SQL 调用已经完成并被填充。我已经尝试过其他想法,以及为 AnswerID 执行 objSqlDataReader,但它没有正常工作。这是我想添加到这个项目中的一个小额外功能,如果我能找到一些帮助来解决我做错的事情,我将非常高兴。提前致谢!
测试 strSQL
是否不是 null
将始终成功,因为您在方法的前面将其设置为非 null
值。
要查看之前完成的检查的记录是否已经存在,您需要检查调用 objSqlDataReader.Read()
的 return 值;它会 return true
只要有额外的行(或者,在这种情况下,第一行)从您的 SELECT
查询中使用。因此,改变这个...
objSqlDataReader.Read();
if (strSQL != null)
{
...到此...
if (objSqlDataReader.Read())
{
作为附加说明,请考虑将 objSqlConnection
、objSqlCommand
和 objSqlDataReader
包装在 using
blocks 中,以确保它们正确 closed/disposed。就像现在一样,当需要完成考试时,你并没有关闭 objSqlDataReader
和 objSqlConnection
,并且根本没有处理 objSqlCommand
。 objSqlDataReader
然后将按如下方式关闭,无论采用 if
的哪个分支...
using (SqlDataReader objSqlDataReader = objSqlCommand.ExecuteReader())
{
if (objSqlDataReader.Read())
{
//objSqlDataReader.Close();// No longer necessary - handled by using
objSqlConnection.Close();
Response.Redirect("StudentExamResults.aspx");
}
else
{
this.Master.MessageForeColor = System.Drawing.Color.Red;
this.Master.Message = "The selected examination has not been completed.";
}
}
假设您不关心内容,而只想检查该行是否存在,您可以这样做:
string sql = "SELECT COUNT(AnswerID) FROM Question ........ WHERE ......";
using (var connection = CreateConnection()) {
using (var cmd = new SqlCommand(sql, connection)) {
bool exists = (int) cmd.ExecuteScalar() > 0;
if (exists) {
Response.Redirect("StudentExamResults.aspx");
} else {
// Do the other thing
}
}
}