无法等待 'int' async/await c#
Cannot await 'int' async/await c#
我正在尝试使用异步和等待,所以我编写了这个简单的代码来感受一下。我正在编写一个 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
但我收到错误提示 "cannot await int"。我该如何解决这个问题谢谢你的帮助。
使用 ExecuteNonQueryAsync
方法调用带有 CancellationToken.None
的重载方法 ExecuteNonQueryAsync(CancellationToken cancellationToken)
。这些方法returns是你Task<int>
需要的。
我正在尝试使用异步和等待,所以我编写了这个简单的代码来感受一下。我正在编写一个 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
但我收到错误提示 "cannot await int"。我该如何解决这个问题谢谢你的帮助。
使用 ExecuteNonQueryAsync
方法调用带有 CancellationToken.None
的重载方法 ExecuteNonQueryAsync(CancellationToken cancellationToken)
。这些方法returns是你Task<int>
需要的。