vakho

[ADO.NET] Asynchronous Processing

Jan 16th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data.SqlClient;
  7.  
  8. namespace ConsoleApplication2
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string connectionString = "Server=VAKHO-PC; Database=TestDB; Integrated Security=true; Asynchronous Processing=true;";
  15.  
  16.             SqlConnection con = new SqlConnection(connectionString);
  17.  
  18.             string sqlCommand = "UPDATE dbo.Users SET salary=salary+10; " + "WAITFOR DELAY '0:0:10'" + " UPDATE dbo.Users SET salary=salary+10";
  19.  
  20.             SqlCommand updateCommand = new SqlCommand(sqlCommand, con);
  21.  
  22.             con.Open();
  23.             Console.WriteLine("Connection Status: " + con.State.ToString());
  24.  
  25.             IAsyncResult result = updateCommand.BeginExecuteNonQuery();
  26.  
  27.             while (!result.IsCompleted)
  28.             {
  29.                 Console.WriteLine(DateTime.Now.ToLongTimeString());
  30.                 System.Threading.Thread.Sleep(2000);
  31.             }
  32.  
  33.             updateCommand.EndExecuteNonQuery(result);
  34.  
  35.             con.Close();
  36.  
  37.             Console.WriteLine("The End");
  38.             Console.ReadKey();
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment