C# 用于循环错误检查
每件事都是有效的,唯一不起作用的是,它为每个账户都做了记录,即使没有为该账户做任何事情/发现任何事情C# 用于循环错误检查,c#,asp.net,C#,Asp.net,每件事都是有效的,唯一不起作用的是,它为每个账户都做了记录,即使没有为该账户做任何事情/发现任何事情 for (int j = 0; j < accounts.Length; j++) { SqlConnection connection = new SqlConnection(SqlDataSource1.ConnectionString); SqlCommand SqlComm = new SqlCommand("ins
for (int j = 0; j < accounts.Length; j++)
{
SqlConnection connection = new SqlConnection(SqlDataSource1.ConnectionString);
SqlCommand SqlComm = new SqlCommand("insert into TableHistory (id, startDate, dueDate, userid, amount, number, needsReview, offer, dateAdded, dateRejected, rejectedBy) " +
"select (select max(id) + 1 from Table1), startDate, dueDate, userid, amount, number, needsReview, offer, dateAdded, GETDATE(), @userID from Table1 where number = @number " +
"delete from Table1 where number = @number", connection);
SqlComm.Parameters.Add("@number", SqlDbType.Int).Value = Convert.ToInt32(accounts[j].ToString());
SqlComm.Parameters.Add("@userID", SqlDbType.Int).Value = userID;
connection.Open();
SqlComm.ExecuteNonQuery();
connection.Close();
NoteAccount note = new NoteAccount("Personal Note", accounts[j].ToString(), userID);
note.makeNote();
}
假设账户11在表1中没有任何内容。没有任何东西会失败,因为insert语句不会激发,因为select语句不会从表1返回任何内容,这很好
但是注意,makeNote;在不需要添加个人备忘时激发并向帐户添加个人备忘。如果帐户存在于表1中,是否有一种简单的方法可以只添加便笺?ExecuteOnQuery将返回受影响的行数,以便您可以将支票放在该行数的基础上。如果大于0,则记下
if(SqlComm.ExecuteNonQuery()>0)
{
NoteAccount note = new NoteAccount("Personal Note", accounts[j].ToString(), userID);
note.makeNote();
}
ExecuteOnQuery将返回受影响的行数,以便您可以在此基础上进行检查。如果大于0,则记下
if(SqlComm.ExecuteNonQuery()>0)
{
NoteAccount note = new NoteAccount("Personal Note", accounts[j].ToString(), userID);
note.makeNote();
}
如果我理解正确,我认为您可以使用ExecuteOnQuery的返回结果。这将返回受查询影响的行数,因此您可以测试该行数是否大于0,并仅在大于0时做注释。如果我理解正确,我认为您可以使用ExecuteOnQuery的返回结果。这将返回受查询影响的行数,因此您可以测试该行数是否大于0,并仅在大于0时做注释。检查ExecuteOnQuery的返回值,它将返回受影响的记录数,然后您可以使用if and调用或避免调用note.makeNote@詹姆斯:另一方面,你不需要打开/关闭连接,而是在循环中创建一个新命令。查看如何使用资源的using block,特别是连接/命令,以便在完成后将其清除。检查ExecuteOnQuery的返回值,它应该返回受影响记录的数量,然后您可以使用if and调用或避免调用note。makeNote@詹姆斯:另一方面,你不需要打开/关闭连接,而是在循环中创建一个新命令。看看如何使用block来使用资源,特别是连接/命令,这样一旦完成它就会被清除。该死的,你比我快了36秒。我需要让SqlComm.ExecuteNonQuery运行两次,还是它实际上会从if语句中运行?@JamesWilson不需要运行两次。它将在if语句中运行。但在这之后就要关闭连接了。该死的,你比我快了36秒。我需要让SqlComm.ExecuteNonQuery运行两次,还是它真的会从if语句中运行?@JamesWilson不需要运行两次。它将在if语句中运行。但在这之后,我们的关系很密切。