简体   繁体   English

当返回的数据集包含一个空行时,Rows.Count = 1

[英]Rows.Count = 1 when returned dataset consists of an empty row

My query does not return anything but an empty row, since there is no records that meet all the criteria of my query. 我的查询只返回一个空行,因为没有符合我查询所有条件的记录,因此不返回任何内容。 However, Rows.Count is 1 instead of 0. Here is my query: 但是,Rows.Count是1而不是0。这是我的查询:

SELECT LicenseID 
FROM LICENSES 
WHERE LicenseType = @LicenseType 
    AND DriverID = @DriverID 
    AND CarID IN(
        SELECT CarID 
        FROM CARS 
        WHERE CarSerial = @CarSerial 
            AND DriverID = @DriverID
    ) AND LicenseID IS NOT NULL 
    AND ((StartDate <= CONVERT(DATETIME, @EndDate)) 
    AND (EndDate >= CONVERT(DATETIME, @StartDate)))

Here is my C# if statement condition that for some reason keeps being true due to row count, even though there is no record meeting criteria: 这是我的C#if语句条件,即使没有记录满足条件,由于行数的原因,该条件由于某些原因也会保持为真:

if (ds != null && ds.Tables[0].Rows.Count > 0)

The database used is SQL Server. 使用的数据库是SQL Server。 This previously seemed to work correctly under Oracle. 以前,这在Oracle下似乎可以正常工作。

To Avoid ERROR: 为避免错误:

if (ds.Tables.Count > 0)
 {
      if (ds.Tables[0].Rows.Count > 0)
      {
         //Then Return a value here or Get the date
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    String getFirstRow = dr[0].ToString();
                    //And so on........
                }
      }
      else
      { 
            //Return value if no rows found.
      }
 }

what I needed to add was just an additional check for "LicenseID <> ''". 我需要添加的只是对“ LicenseID <>”的额外检查。 "LicenseID IS NOT NULL" was not enough since there has been a record meeting all criteria but having LicenseID = "". “ LicenseID IS NOT NULL”是不够的,因为有一条记录满足所有条件,但LicenseID =“”。 I believe this has not been an issue with Oracle database, but turned out to be an issue after switching to SQL Server. 我相信这不是Oracle数据库的问题,但在切换到SQL Server之后却成为问题。 Thanks for all your comments and suggestions. 感谢您的所有意见和建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM