Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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# 当返回的数据集包含空行时,Rows.Count=1_C#_Sql Server_Dataset_Rowcount - Fatal编程技术网

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

C# 当返回的数据集包含空行时,Rows.Count=1,c#,sql-server,dataset,rowcount,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

我的查询只返回一个空行,因为没有满足查询所有条件的记录。但是,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)))
使用的数据库是SQL Server。这在以前的Oracle下似乎可以正常工作。

为避免错误:

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.
      }
 }
要避免错误,请执行以下操作:

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

我需要添加的只是对“LicenseID”的额外检查。“LicenseID不为空”是不够的,因为有一个记录满足所有条件,但具有LicenseID=“”。我相信这并不是Oracle数据库的问题,而是在切换到SQL Server之后的问题。谢谢您的评论和建议。

我需要添加的只是对“LicenseID”的额外检查。“LicenseID不为空”是不够的,因为有一个记录满足所有条件,但具有LicenseID=“”。我相信这并不是Oracle数据库的问题,而是在切换到SQL Server之后的问题。感谢您的评论和建议。

空行仍然是一行。在sql中尝试一些isnull?如果没有满足查询所有条件的记录,那么查询将返回零行,而不是空行。所以,很明显,您做错了什么。如果返回了1行,您能看到值是多少吗?也许这有助于排除故障。基本上,当执行这行代码时,它有一行数据,所以请找出它是什么。如果数据库有空值,则此代码“if(ds!=null&&ds.Tables[0].Rows.Count>0”将返回一个错误。@Trey-我的SQL已经有isnull。空行仍然是一行。在sql中尝试一些isnull?如果没有满足查询所有条件的记录,那么查询将返回零行,而不是空行。所以,很明显,您做错了什么。如果返回了1行,您能看到值是多少吗?也许这有助于排除故障。基本上,当执行这行代码时,它有一行数据,所以请找出它是什么。如果数据库有空值,则此代码“if(ds!=null&&ds.Tables[0].Rows.Count>0”将返回一个错误。@Trey-我的SQL已经有isnull了。