c# - 无法等待'int'异步/等待C#

标签 c# asynchronous

我正在尝试使用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/

相关文章:

c# - 依赖注入(inject) .NET Core - InvalidOperationException

javascript - 使用 Ajax 在 ExtJS 中进行异步数据检索和渲染

asp.net - Web 应用程序中无需等待的异步 HttpWebRequest

java - 如何获取并设置 "messaging provider"以便 JAXM 客户端调用异步 SOAP API?

c# - HttpWebRequest 的响应不显示 UTF-8 符号

c# - log4net 在每一行打印日期时间/调用类/函数

c# - MySQL 连接到 phpMyAdmin 用 C# 创建的数据库

c# - 停止旋转时如何检测图像的位置?

c# - 寻找 OO 大师,在设计我的编程逻辑时需要一些帮助。没什么特别的,只是新的

c# - Azure 表存储查询从错误分区返回数据?