当返回的数据集由空行组成时,Rows.Count = 1

时间:2017-04-10 21:05:30

标签: c# sql-server dataset rowcount

我的查询不会返回任何空行,因为没有符合查询所有条件的记录。但是,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)))

这是我的C#if语句条件,由于某些原因由于行计数而保持为真,即使没有记录满足条件:

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

使用的数据库是SQL Server。以前这似乎在Oracle下正常工作。

2 个答案:

答案 0 :(得分:0)

避免错误:

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.
      }
 }

答案 1 :(得分:0)

我需要添加的内容只是对“LicenseID&lt;&gt;''”的额外检查。 “LicenseID IS NOT NULL”是不够的,因为已经有记录符合所有条件,但LicenseID =“”。我相信这对Oracle数据库来说不是问题,但在切换到SQL Server之后却成了问题。感谢您的所有意见和建议。

相关问题