0

我有一个问题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();
4

1 回答 1

0

看起来您Card_reg在数据库中定义了该表,但该表中不包含任何值 - 它是空的。这就是select * from Card_reg查询不返回任何结果并且数据读取器没有可读取的内容的原因。

于 2013-07-06T10:19:02.443 回答