我有一个问题SqlDataReader
。
这是我的代码。问题是当我调试它时,它不会走得更远
while(dr.read())
这意味着返回的值dr.Read()
是假的。我不知道怎么了。
if (CS_time_date[i].Substring(0, 2) == "CS")
{
string cardserial = CS_time_date[i].Substring(4, 5);
try
{
using (SqlConnection con = new SqlConnection(WindowsAppEmp.Properties.Settings.Default.Database1ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from Card_reg ", con);
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
if ((dr["Cardserial"].ToString() == cardserial))
{
flag = 1;
string EmpID = dr["Id"].ToString();
string F_Name = dr["fname"].ToString();
string L_Name = dr["lname"].ToString();
// Insert_EmpReport(EmpID, F_Name, L_Name, cardserial, CS_time_date,con);
string strsql1 = "Insert into Emp_Report (EmpId,CS,fname,lname,CheckIn,CheckOut,Date,Status) values (@EmpID,@Cs,@fname,@lname,@CheckIn,@CheckOut,@Date,@Status)";
SqlCommand report_cmd = new SqlCommand(strsql1, con);
report_cmd.Parameters.AddWithValue("@EmpID", EmpID);
report_cmd.Parameters.AddWithValue("@Cs", cardserial);
report_cmd.Parameters.AddWithValue("@fname", F_Name);
report_cmd.Parameters.AddWithValue("@lname", L_Name);
if (CS_time_date[i].Substring(9, 10) == "56")
if (CS_time_date[i + 2] == DateTime.Now.ToString("dd/mm/yyyy"))
{
report_cmd.Parameters.AddWithValue("@CheckIn", CS_time_date[i + 1]);
report_cmd.Parameters.AddWithValue("@Status", "Present");
}
else
report_cmd.Parameters.AddWithValue("@Status", "Absent");
else
report_cmd.Parameters.AddWithValue("@CheckOut", CS_time_date[i + 1]);
report_cmd.Parameters.AddWithValue("@Date", CS_time_date[i + 2]);
}
else
flag = 0;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
} while (reader.Peek() != -1);
reader.Close();