我正在尝试使用async和await,所以我写了这个简单的代码来感受事物.我正在写一个web api和Post函数调用这个包装的storedprocedure.
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
Run Code Online (Sandbox Code Playgroud)
但我得到错误说"不能等待int".我如何解决这个问题,谢谢你的帮助.
使用ExecuteNonQueryAsync
它调用重载方法方法ExecuteNonQueryAsync(CancellationToken cancellationToken)
用CancellationToken.None
.这些方法可以满足Task<int>
您的需求.