[英]SqlDataReader in C#
I have a problem with SqlDataReader
. 我有
SqlDataReader
的问题。
Here's my code. 这是我的代码。 the problem is when I debug it it doesn't go further than
问题是,当我调试它时,它不会超出
while(dr.read())
it means the value returned by dr.Read()
is false. 这意味着
dr.Read()
返回的dr.Read()
false。 I don't know what's wrong. 我不知道怎么了
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();
It looks like you have the Card_reg
table defined in your database but this table doesn't contain any values in it - it is empty. 看起来您在数据库中定义了
Card_reg
表,但是该表中不包含任何值-它为空。 That's why the select * from Card_reg
query doesn't return any results and the data reader has nothing to read. 这就是为什么
select * from Card_reg
查询中的select * from Card_reg
不返回任何结果,并且数据读取器无法读取的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.