SqlDataReader没有返回所有行

时间:2013-09-02 11:51:35

标签: c# sql-server c#-4.0 ado.net

我的SQL命令返回3行,这些行在SQL Server GUI中得到验证。我运行完全相同的代码,SqlDataReader只返回其中的2个。相同的sql命令返回带有SqlDataAdapter的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()) {}

5 个答案:

答案 0 :(得分:2)

你可以创建一个类..

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 :(得分:1)

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

时有一些指导原则

尝试使用此代码:

{{1}}

每次都适合我。

答案 2 :(得分:0)

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

DataSet ds = new DataSet();

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

使用部分中的一行是读取第一条记录。在while循环中,它从第二个记录读取。我删除了以下if条件,它工作正常。谢谢大家的回复。很抱歉没有发布该行,因为我认为该行只处理异常。

if(!reader.Read())  抛出新的ApplicationException(“由金融机构返回的MISSING事务。在数据库中找不到事务。”); while(reader.Read()) {}

相关问题