0
我有一個SqlDataReader
的問題。C#中的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();
「Card_reg」表中是否有記錄? –
您提供的代碼比您真正需要的要多得多。請把它縮小到顯示問題所需的位數。例如,在'while'循環中,您不需要任何其他更多的診斷記錄...請閱讀http://tinyurl.com/so-hints和http://tinyurl.com/so-list –
是我的數據庫文件中有Card_reg表 –