4

我的 SQL 命令返回在 SQL Server GUI 中验证的 3 行。我运行完全相同的代码,SqlDataReader 只返回其中 2 个。相同的 sql 命令返回 3 行,带有SqlDataAdapter.

这是我的代码 -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);
}

我找到了解决方案: using section 中的一行是读取第一条记录。在 while 循环中,它正在读取第二条记录。我删除了以下 if 条件,它工作正常。谢谢大家的回复。很抱歉没有发布该行,因为我认为该行仅处理异常。

if (!reader.Read()) 
    throw new ApplicationException("MISSING Transaction Returned By Financial Institution. Transaction was not found in the database."); 
while (reader.Read()) {}
4

5 回答 5

3

你可以创建一个类..

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。如果是这样,请告诉我,我将更新代码。

于 2013-09-02T12:45:02.817 回答
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
        }
    }  
}

每次都为我工作。

于 2013-09-02T12:12:33.297 回答
0
 con.Open();    
    SqlDataReader reader = command.ExecuteReader();    
         while (reader.Read())    
      {

         // Do stuff here     
      }
    con.close();
于 2013-09-02T12:58:01.393 回答
0

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

if (!reader.Read()) throw new ApplicationException("MISSING Transaction Returned By Financial Institution. Transaction was not found in the database."); 而 (reader.Read()) {}

于 2013-09-03T05:40:46.607 回答
0

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

DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter(sql, connection);
da.Fill(ds);
于 2013-09-02T12:20:15.953 回答