我的 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()) {}