本文整理汇总了C#中SqlCommand.ExecuteScalarAsync方法的典型用法代码示例。如果您正苦于以下问题:C# SqlCommand.ExecuteScalarAsync方法的具体用法?C# SqlCommand.ExecuteScalarAsync怎么用?C# SqlCommand.ExecuteScalarAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SqlCommand
的用法示例。
在下文中一共展示了SqlCommand.ExecuteScalarAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MARSAsyncTimeoutTest
public static void MARSAsyncTimeoutTest()
{
using (SqlConnection connection = new SqlConnection(s_yukonConnectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("WAITFOR DELAY '01:00:00';SELECT 1", connection);
command.CommandTimeout = 1;
Task<object> result = command.ExecuteScalarAsync();
Assert.True(((IAsyncResult)result).AsyncWaitHandle.WaitOne(30 * 1000), "Expected timeout after one second, but no results after 30 seconds");
Assert.True(result.IsFaulted, string.Format("Expected task result to be faulted, but instead it was {0}", result.Status));
Assert.True(connection.State == ConnectionState.Open, string.Format("Expected connection to be open after soft timeout, but it was {0}", connection.State));
Type type = typeof(SqlDataReader).GetTypeInfo().Assembly.GetType("System.Data.SqlClient.TdsParserStateObject");
FieldInfo field = type.GetField("_skipSendAttention", BindingFlags.NonPublic | BindingFlags.Static);
Assert.True(field != null, "Error: This test cannot succeed on retail builds because it uses the _skipSendAttention test hook");
field.SetValue(null, true);
try
{
SqlCommand command2 = new SqlCommand("WAITFOR DELAY '01:00:00';SELECT 1", connection);
command2.CommandTimeout = 1;
result = command2.ExecuteScalarAsync();
Assert.True(((IAsyncResult)result).AsyncWaitHandle.WaitOne(30 * 1000), "Expected timeout after six or so seconds, but no results after 30 seconds");
Assert.True(result.IsFaulted, string.Format("Expected task result to be faulted, but instead it was {0}", result.Status));
// Pause here to ensure that the async closing is completed
Thread.Sleep(200);
Assert.True(connection.State == ConnectionState.Closed, string.Format("Expected connection to be closed after hard timeout, but it was {0}", connection.State));
}
finally
{
field.SetValue(null, false);
}
}
}