如果条件为真,则中断或关闭 sqlDatareader 并退出使用 (SqlDataReader rdr = cmd.E
本文关键字:退出 SqlDataReader rdr cmd sqlDatareader 条件 中断 如果 | 更新日期: 2023-09-27 17:56:47
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while(rdr.Read())
{
DateTime dt2 = Convert.ToDateTime(rdr["Date"]);
int Classroom = Convert.ToInt32(rdr["ClassroomNo"]);
TimeSpan ST = (TimeSpan)rdr["StartingTime"];
TimeSpan ET = (TimeSpan)rdr["EndingTime"];
TimeSpan span = ET.Subtract(ST);
double Minutesdifference = span.TotalMinutes;
TimeSpan NewTime = (TimeSpan)rdr["StartingTIme"];
for (int i = 1; i <= Minutesdifference; i++)
{
NewTime = NewTime.Add(TimeSpan.FromMinutes(1));
// Response.Write(NewTime + "<br>");
Response.Write(Classroom + "<br>");
Response.Write(ClassroomNo + "<br>");
Response.Write(dt + "<br>");
Response.Write(dt2 + "<br>");
Response.Write(StartTime + "<br>");
Response.Write(NewTime + "<br>");
Response.Write(ST + "<br>");
if ((Classroom == ClassroomNo && dt == dt2 && StartTime == NewTime) || (Classroom == ClassroomNo && dt == dt2 && StartTime == ST))
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
}
}
if (flag == 0)
{
SqlCommand cmd2 = new SqlCommand("spStoreTimeSlotDetails2", con);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("@Username", hfUsername.Value);
cmd2.Parameters.AddWithValue("@EmpName", lblFacultyName.Text);
cmd2.Parameters.AddWithValue("@Date", dt);
cmd2.Parameters.AddWithValue("@Day", day);
cmd2.Parameters.AddWithValue("@ST", StartTime);
cmd2.Parameters.AddWithValue("@ET", EndTime);
cmd2.Parameters.AddWithValue("@TimeSlot", hfTmeSlot.Value);
cmd2.Parameters.AddWithValue("@Topic", txtTopic.Text);
cmd2.Parameters.AddWithValue("@ClassroomNo", ClassroomNo);
cmd2.ExecuteNonQuery();
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Data Saved Successfully";
}
else if (flag == 1)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Time Slot already allocated by some other Employee. Please choose another";
}
我将 If 条件写入 for 循环。如果该条件为 true,则中断 for 循环,但while(rdr.Read())
方法仍在执行。我只想关闭 Reader 方法并直接从using(sqlDataReader rdr=cmd.ExecuteReader)
方法中出来并执行 if(flag==0)。
我的假设是,在脱离for循环后,你仍然想在if loop of flag
中执行代码,所以只需添加这段代码就可以打破rdr.Read()
while循环
else if (flag == 1)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Time Slot already allocated by some other Employee. Please choose another";
break;
}
此代码将确保释放和关闭 Reader 对象:
using (SqlDataReader rdr = cmd.ExecuteReader())
因此,除了建议的更改之外,不需要太多
while (reader.Read() && !flag)
{
....
for (int i = 1; i <= Minutesdifference; i++)
{
if ((Classroom == ClassroomNo && dt == dt2 && StartTime == NewTime) || (Classroom == ClassroomNo && dt == dt2 && StartTime == ST))
{
flag = 1;
break;
}
else
{flag=0;}
...
}
}
正如谢尔盖·叶夫拉科夫所建议的,在 while 循环之前将标志变量设置为 0,如下所示。
using (SqlDataReader rdr = cmd.ExecuteReader())
{
flag=0;
while (reader.Read() && !flag)
{
.....
for (int i = 1; i <= Minutesdifference; i++)
{
if ((Classroom == ClassroomNo && dt == dt2 &&
StartTime== NewTime) || (Classroom == ClassroomNo &&
dt == dt2 && StartTime == ST))
{
flag = 1;
break;
}
//no need of else part here now.
...
}
}
}