2011-11-02 55 views
0

所以我有这个类,有C#功能与预留和释放席位一起工作。无论如何,在调试模式下,while被检查,然后突然其内容被跳过。我认为没有结果集返回读取实际访问?但我能做什么?因为这段代码在工作,突然就停止...c#不能访问阅读器sql

string queryRangeForCheck = "select * from DateTest WHERE datefill between @startdate AND @enddate"; 
     SqlConnection conn = new SqlConnection("...."); 
     conn.Open(); 
     SqlCommand command = new SqlCommand(queryRangeForCheck, conn); 
     command.Parameters.AddWithValue("@startdate", startDate); 
     command.Parameters.AddWithValue("@enddate", endDate); 
     SqlDataReader reader = command.ExecuteReader(); 

     int bookingStatus = 2; 
     //for the range found do: 
     while (reader.Read()) 
     { 
      //establish the number of seats available for this date 
      int currentRow = Convert.ToInt32(reader["seats"]); 
      //if the number is > 0 i.e. if not fully booked 

      if (currentRow != 0) 
      { 
       //if the number of seats can accomodate the number required 
       if (currentRow >= requiredSeats) 
       { 
        .... 

请指点 感谢

+2

唯一奇怪/坏你的代码是,你不括在'using'声明你的数据库连接,以及是否出售,或不晚于目前还不清楚。 – Icarus

+1

如果它跳过了,那么没有与DB中的条件相匹配的数据...因此,您要么检查数据库中的数据,要么处理代码中的这种情况... – Yahia

+0

我会处理它,但我不知道使用事情......因为在C#中很新颖(就像一个月新)。将确保我添加它。因为日期是作为函数参数发送的,所以我发现它们的格式不是预期的,因此是错误。仍在努力。 –

回答

0

尝试检查,如果读者有使用HasRows属性行。

此外,创建一个新表并使用阅读器加载表。这将关闭阅读器,但您将能够使用数据集调试可视化工具来检查结果。然后再次执行该命令。在以下代码片段中,该命令返回多个结果集。因此,在调用Load方法期间,我一直加载表,直到读取器关闭。

  oReader = Me.SelectCommand.ExecuteReader(CommandBehavior.CloseConnection Or CommandBehavior.KeyInfo) 

#If DbDebug Then 

     'load method internally advances to the next result set 
     'therefore, must check to see if reader is closed instead of calling next result 

     Do 
      Dim oTable As New DataTable("Table") 
      oTable.Load(oReader) 
      'Inspect oTable here as needed. 
      oTable.WriteXml("C:\" + Environment.TickCount.ToString + ".xml") 
      oTable.Dispose() 
     Loop While oReader.IsClosed = False 

     'must re-open the connection 
     Me.SelectCommand.Connection.Open() 

     'reload data reader 
     oReader = Me.SelectCommand.ExecuteReader(CommandBehavior.CloseConnection Or CommandBehavior.KeyInfo) 

#End If 
0

主要问题被证实是读者没有返回任何东西。这是由于通过和期望的日期参数的类型和格式。因此它不匹配,并且未能返回任何结果。当日期以mm/dd/yyyy重新格式化时,它实际上全部都有效。感谢大家的大力投入:)

+0

前关闭数据读取器字符串不应该导致格式问题。 –