SqlDataReader没有返回所有行

4
我的SQL命令在SQL Server GUI中返回了3行结果。但是当我运行完全相同的代码时,SqlDataReader只返回其中的2行结果。使用SqlDataAdapter执行相同的SQL命令则返回了3行结果。
以下是我的代码 - 数据集ds包含3行数据。为了展示不同之处,我添加了SqlDataAdapter
提前感谢您的帮助。
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["VPO"].ConnectionString))
{
    string sql = "SELECT DISTINCT A.account_id, A.fname, A.lname, 
                  FROM T_Test1 A WITH (NOLOCK) 
                  JOIN T_Test2 AF WITH (NOLOCK) ON A.Account_id=AF.Account_id
                  WHERE account_status = 'A' AND A.card IS NOT NULL
                    AND A.dateFrom >= '09-02-2013 00:00:00' 
                    AND A.dateFrom <= '09-30-2013 00:00:00' 
                    AND AF.code = 'INE'";

    SqlCommand command = new SqlCommand(sql.ToString(), connection);
    command.CommandTimeout = 3600;

    connection.Open();

    using (SqlDataReader reader = command.ExecuteReader())
    { 
        while (reader.Read())
        {}
    }

    DataSet ds = new DataSet();

    SqlDataAdapter da = new SqlDataAdapter(command.CommandText, connection);
    da.Fill(ds);
}

我找到了解决方法: 在使用部分中,有一行代码读取第一个记录。在while循环中,它从第二个记录开始读取。我删除了下面的if条件,并且它可以正常工作。感谢您所有人的回复。对于没有发布那行代码的问题表示抱歉,因为我认为该行只处理异常情况。
if (!reader.Read()) 
    throw new ApplicationException("MISSING Transaction Returned By Financial Institution. Transaction was not found in the database."); 
while (reader.Read()) {}

2
你的代码看起来不错,你能把你 while 循环里面写的内容发一下吗? - Satinder singh
我总是使用相同的代码来填充我的列表视图,非常完美!你可能在while循环中漏掉了某些东西。 - Zeeshan
@RajVish 在 while (reader.Read()) 中发布代码。 - Suraj Singh
读取器正在返回所有行,您使用得很正确。但是您的查询未产生正确的行。 - usr
5个回答

3

你可以创建一个类(class)...

public class AccountDetails
{
    public int AccountId {get; set;}  
    public string FName {get; set;}     
    public string LName {get; set;} 
}

然后返回一个类似这样的AccontDetails列表...
public List<AccountDetails> GetAccountDetails()
{
    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["VPO"].ConnectionString))
    {
        string sql = "SELECT DISTINCT A.account_id, A.fname, A.lname, 
                      FROM T_Test1 A WITH (NOLOCK) 
                      JOIN T_Test2 AF WITH (NOLOCK) ON A.Account_id=AF.Account_id
                      WHERE account_status = 'A' AND A.card IS NOT NULL
                        AND A.dateFrom >= '09-02-2013 00:00:00' 
                        AND A.dateFrom <= '09-30-2013 00:00:00' 
                        AND AF.code = 'INE'";

        SqlCommand command = new SqlCommand(sql.ToString(), connection);
        command.CommandTimeout = 3600;

        connection.Open();
        var accDetails = new List<AccountDetails>();

        using (var rdr = command.ExecuteReader())
            {
                    while (rdr.Read())
                    {
                        var accountDetails = new AccountDetails{
                            AccountId = rdr.GetInt32(0),
                            FName = rdr.GetString(1),
                            LName = rdr.GetString(2)
                        };
                        accDetails.Add(accountDetails);
                    }                   
            }
    }

    return accDetails;
}

由于我是手写的,语法可能有误。

与使用 DataSet 相比,这种方法更轻便,除非你确实需要使用 DataSet。如果是这样,请告诉我,我会更新代码。


1
当超出作用域(using块后)时,您不能使用command,但这可能只是SqlDataAdapter的示例。无论如何,在使用SqlDatareader时,有一些指导方针,请尝试使用以下代码:
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["VPO"].ConnectionString))
{
    connection.Open();
    string sql = "SELECT DISTINCT A.account_id, A.fname, A.lname, 
                  FROM T_Test1 A WITH (NOLOCK) 
                  JOIN T_Test2 AF WITH (NOLOCK) ON A.Account_id=AF.Account_id
                  WHERE account_status = 'A' AND A.card IS NOT NULL
                    AND A.dateFrom >= '09-02-2013 00:00:00' 
                    AND A.dateFrom <= '09-30-2013 00:00:00' 
                    AND AF.code = 'INE'";

    using(SqlCommand command = new SqlCommand(sql, connection))
    { 
        command.CommandTimeout = 3600;             
        using (SqlDataReader reader = command.ExecuteReader())
        { 
            while (reader.Read())
            {
                 // Read the data here and do your thing...
            }
            reader.Close(); // We should close this reader - This line is for readability
        }
    }  
}

每次都对我有效。


0

使用 SqlDataAdapter 是错误的。你必须像下面这样使用:这个解决方案可以解决你的问题。

DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter(sql, connection);
da.Fill(ds);

我猜他做了同样的事情... 通过传递CommandText。 - AsitK

0
 con.Open();    
    SqlDataReader reader = command.ExecuteReader();    
         while (reader.Read())    
      {

         // Do stuff here     
      }
    con.close();

0

在使用部分中的一行是读取第一个记录。 在while循环中,它将从第二个记录开始读取。 我删除了下面的if条件,它运行良好。 感谢您所有人的回复。 抱歉没有发布那一行,因为我认为那一行只处理异常。

if (!reader.Read()) throw new ApplicationException("金融机构返回缺少交易。未在数据库中找到交易。"); while (reader.Read()) {}


你应该通过更新问题并提供答案来帮助其他用户获得正确的答案或将该帖子标记为已接受的答案。 - AsitK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接