我正在尝试使用async&await,所以我写了这段简单的代码来感受一下事情。我正在编写一个Web API,Post函数调用此包装的存储过程。
public async Task<bool> AddNotificationEntryStoredProcedure(NotificationEntry NewNotification, String ExternalRefID)
{
try
{
Connection = new SqlConnection(ConnectionString);
Connection.Open();
Command = new SqlCommand("InsertNewMessage", Connection);
Command.CommandType = CommandType.StoredProcedure;
Command.Parameters.AddWithValue("@ExternalReferenceID ", ExternalRefID);
Command.Parameters.AddWithValue("@MessageID", NewNotification.id);
Command.Parameters.AddWithValue("@Recievers", NewNotification.To);
Command.Parameters.AddWithValue("@MessageBody", NewNotification.MessageBody);
Command.Parameters.AddWithValue("@DeliveryType", NewNotification.DelieveryType);
Command.Parameters.AddWithValue("@Response", NewNotification.Feedback);
Command.Parameters.AddWithValue("@CreatedOn", DateTime.Now);
//String TodoDuedate = String.Format("{0:yyyy-MM-dd}", todo.DueDate); // convert date format
await Command.ExecuteNonQuery(); /*here is where it complains*/
return true;
}
catch (Exception )
{
//err.ToString();
return false;
}
finally
{
Connection.Close();
}
}// end add function
但我收到错误消息说“无法等待int”。我该如何解决这个问题,感谢您的帮助。
最佳答案
使用ExecuteNonQueryAsync
方法,该方法使用ExecuteNonQueryAsync(CancellationToken cancellationToken)
调用重载方法CancellationToken.None
。这些方法返回您需要的Task<int>
。
关于c# - 无法等待'int'异步/等待C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28638583/