我的SQL命令在SQL Server GUI中返回了3行结果。但是当我运行完全相同的代码时,SqlDataReader只返回其中的2行结果。使用
以下是我的代码 - 数据集
提前感谢您的帮助。
我找到了解决方法: 在使用部分中,有一行代码读取第一个记录。在while循环中,它从第二个记录开始读取。我删除了下面的if条件,并且它可以正常工作。感谢您所有人的回复。对于没有发布那行代码的问题表示抱歉,因为我认为该行只处理异常情况。
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()) {}
while (reader.Read())
中发布代码。 - Suraj Singh