SHARE
TWEET

Untitled

a guest Oct 21st, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static async IAsyncEnumerable<IEnumerable<object?>> GetResults(this SqlConnection connection, string query, [EnumeratorCancellation] CancellationToken token)
  2. {
  3.     if (string.IsNullOrWhiteSpace(query))
  4.     {
  5.         throw new ArgumentException(nameof(query));
  6.     }
  7.     var wasClosed = connection.State != ConnectionState.Open;
  8.     try
  9.     {
  10.         if (wasClosed)
  11.         {
  12.             await connection.OpenAsync(token);
  13.         }
  14.  
  15.         using var command = new SqlCommand(query, connection);
  16.         using var reader = await command.ExecuteReaderAsync(token);
  17.         if (reader.HasRows)
  18.         {
  19.             while (!token.IsCancellationRequested && await reader.ReadAsync(token))
  20.             {
  21.                 var row = new List<object?>(reader.FieldCount);
  22.                 for (var i = 0; i < reader.FieldCount; i++)
  23.                 {
  24.                     var value = await reader.IsDBNullAsync(i) ? null : reader.GetValue(i);
  25.                     row.Add(value);
  26.                 }
  27.                 yield return row;
  28.             }
  29.         }
  30.     }
  31.     finally
  32.     {
  33.         if(wasClosed)
  34.         {
  35.             connection?.Close();
  36.         }
  37.     }            
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top