Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查datareader并将其与另一个值进行比较?_C#_Ms Access 2010 - Fatal编程技术网

C# 如何检查datareader并将其与另一个值进行比较?

C# 如何检查datareader并将其与另一个值进行比较?,c#,ms-access-2010,C#,Ms Access 2010,我想检查我的数据读取器值,并将其与另一个值进行比较。我使用这种代码的和平,但它不起作用 ol_com.CommandText = "select [card_id] from student_info where [card_id] = '" + card_No.Text + "'"; reader = ol_com.ExecuteReader(); if (reader.IsDBNull(0) && reader["card_id"] == "-") { //do my

我想检查我的数据读取器值,并将其与另一个值进行比较。我使用这种代码的和平,但它不起作用

ol_com.CommandText = "select [card_id] from student_info where [card_id] = '" + card_No.Text + "'";
reader = ol_com.ExecuteReader();
if (reader.IsDBNull(0) && reader["card_id"] == "-")
{
   //do my work here
}//end if

else
{
    //give a message
}//end else

ol_com.CommandText = "select [card_id] from student_info where [card_id] = '" + card_No.Text + "'";
reader = ol_com.ExecuteReader();
if (reader.IsDBNull(0) && reader["card_id"] == "-")
{
   //do my work here
}//end if

else
{
    //give a message
}//end else
SqlDataReader的默认位置在第一条记录之前。因此,您必须调用Read来开始访问任何数据。

ol_com.CommandText = "select [card_id] from student_info where [card_id] = '" + card_No.Text + "'";
reader = ol_com.ExecuteReader();
if (reader.IsDBNull(0) && reader["card_id"] == "-")
{
   //do my work here
}//end if

else
{
    //give a message
}//end else
因此,必须调用Read()函数才能获得第一条记录

ol_com.CommandText = "select [card_id] from student_info where [card_id] = '" + card_No.Text + "'";
reader = ol_com.ExecuteReader();
if (reader.IsDBNull(0) && reader["card_id"] == "-")
{
   //do my work here
}//end if

else
{
    //give a message
}//end else
Read()方法返回一个布尔值,如果返回任何行,则返回true;如果未返回任何行,则返回false

ol_com.CommandText = "select [card_id] from student_info where [card_id] = '" + card_No.Text + "'";
reader = ol_com.ExecuteReader();
if (reader.IsDBNull(0) && reader["card_id"] == "-")
{
   //do my work here
}//end if

else
{
    //give a message
}//end else
ol_com.CommandText = "select [card_id] from student_info where [card_id] = '" + card_No.Text + "'";
reader = ol_com.ExecuteReader();
if(reader.Read())
{
  if (reader.IsDBNull(0) || reader["card_id"].ToString() == "-")
  {
   //do my work here
  }//end if

  else
  {
    //give a message
  }//end else
}

else
{
  //give message that given value does not exist in database
}

并且您应该使用| |(OR)运算符,而不是&(and),因为值不能同时是DBNull“-”。它只能是DBNull“-”

任何异常或错误消息?请使用。这种字符串连接对SQL注入攻击是开放的。您错过了
reader.Read()
您没有调用
Read
,因此您没有进入第一个结果。此外,您使用的是读卡器的索引器,它具有声明类型的
对象
,但您使用
=
将其与
字符串
进行比较。这不是你想要的。如果我使用或不使用sql参数对我来说都不是问题。。。我的问题是这段代码没有我需要的,它给了我一个错误说没有列/行存在Jon skeet。。。可以给我一些代码。。。我怎么做?如果我输入“-”,它会给我一条消息,如果我输入的另一个值不在card_id列中,它会给出错误消息说行/列不存在数据?检查reader.Read()的返回值以检查是否有记录请参阅我在你的开场白中的评论
ol_com.CommandText = "select [card_id] from student_info where [card_id] = '" + card_No.Text + "'";
reader = ol_com.ExecuteReader();
if (reader.IsDBNull(0) && reader["card_id"] == "-")
{
   //do my work here
}//end if

else
{
    //give a message
}//end else