我期待我的代码要做的是收集前十条记录,其中有一个' I'在SQLMigrationFl字段中,然后删除那些' I'值。下面是处理此问题的代码部分。
string inProgressQuery = "SELECT TOP 10 IDType, ID, Program, Date, Body,
AcctNo, ANPACAcctID, ANPACClientID, TEAMID,
ImportDate, AnnualReview, TeamGUID,
ANPACClientLastName, ANPACClientFirstName, " +
"PolicyNumber, AccountOwnerLastName,
AccountOwnerFirstName, SCRACF, SCDateTime, NoteID
FROM NoteTable WHERE SQLMigrationFl = ?";
command = new OleDbCommand(inProgressQuery, connection);
command.Parameters.AddWithValue("SQLMigrationFl", "I");
reader = command.ExecuteReader();
if(reader.HasRows)
{
while(reader.Read())
{
//clear the In Progress flag
query = "UPDATE NoteTable SET SQLMigrationFl = ? WHERE
NoteTable.NoteID = " + reader[19].ToString();
command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("SQLMigrationFl", DBNull.Value);
reader = command.ExecuteReader();
}
}
我发现的是查询返回一个值并对其进行处理。然后在五秒钟内找到另一条记录并重新处理该记录。 *五秒钟只是我们在代码中设置的延迟,以检查要处理的更多记录。它一次处理一条记录,而不是抓取十条记录并在同一个while循环中同时处理这些记录。我的代码或查询有问题吗?
感谢您的帮助。
答案 0 :(得分:1)
罪魁祸首是您使用reader = command.ExecuteReader();
重新分配数据阅读器。现在返回1个结果,下一个循环将超过1个结果。无论非SELECT查询,请使用ExecuteNonQuery。
您可以将代码更新为"更好"的其他地方是
using
块中包装实现IDisposable的类型实例,以确保清理资源。我还建议您不要共享连接实例,下面似乎某处可能存在静态连接。最好不要在需要时共享一个并创建/打开一个,然后关闭/处理它。
string inProgressQuery = "SELECT TOP 10 IDType, ID, Program, Date, Body,
AcctNo, ANPACAcctID, ANPACClientID, TEAMID,
ImportDate, AnnualReview, TeamGUID,
ANPACClientLastName, ANPACClientFirstName, " +
"PolicyNumber, AccountOwnerLastName,
AccountOwnerFirstName, SCRACF, SCDateTime, NoteID
FROM NoteTable WHERE SQLMigrationFl = ?";
using(var command = new OleDbCommand(inProgressQuery, connection))
{
// I guessed on the type and length
command.Parameters.Add(new OleDbParameter("SQLMigrationFl", OleDbType.VarChar, 10)).Value = "I";
using(var reader = command.ExecuteReader())
{
while(reader.Read())
{
//clear the In Progress flag
const string UpdateQuery = "UPDATE NoteTable SET SQLMigrationFl = ? WHERE NoteTable.NoteID = ?";
using(var commandUpdate = new OleDbCommand(UpdateQuery, connection))
{
commandUpdate.Parameters.Add(new OleDbParameter("SQLMigrationFl", OleDbType.VarChar, 10)).Value = DBNull.Value;
commandUpdate.Parameters.Add(new OleDbParameter("NoteId", OleDbType.Int)).Value = reader[19];
commandUpdate.ExecuteNonQuery();
}
}
}
}