vakho

ADO.NET Asynchronous Calls

Nov 1st, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10.  
  11. namespace TestADO
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         SqlConnection con = new SqlConnection(@"Server=LAB-418-11-PC\STUDENT; Database=Test_DB; Integrated Security=true; Asynchronous Processing=true;");
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             int id = Convert.ToInt32(textBox1.Text);
  24.             string name = textBox2.Text;
  25.             string surname = textBox3.Text;
  26.  
  27.             SqlCommand updateCommand = new SqlCommand("UPDATE Users SET name=N'" + name + "', surname=N'" + surname + "' WHERE id=" + id + "; WAITFOR DELAY 0:0:5; " + "UPDATE Users SET name=N'" + name + "', surname=N'" + surname + "' WHERE id=" + id, con);
  28.             con.Open();
  29.  
  30.             IAsyncResult result = updateCommand.BeginExecuteNonQuery();
  31.  
  32.             while (!result.IsCompleted)
  33.             {
  34.                 label1.Text = DateTime.Now.ToLongTimeString();
  35.                 System.Threading.Thread.Sleep(1000);
  36.             }
  37.         updateCommand.EndExecuteNonQuery(result);
  38.             MessageBox.Show("Completed!");
  39.             con.Close();
  40.         }
  41.  
  42.         private void button2_Click(object sender, EventArgs e)
  43.         {
  44.             string name = textBox2.Text;
  45.             string surname = textBox3.Text;
  46.  
  47.             SqlCommand insertCommand = new SqlCommand("INSERT INTO Users (name, surname) OUTPUT INSERTED.id VALUES(N'" + name + "', N'" + surname + "')", con);
  48.             con.Open();
  49.  
  50.             Object obj = insertCommand.ExecuteScalar();
  51.  
  52.             MessageBox.Show(obj.ToString());
  53.             con.Close();
  54.         }
  55.  
  56.         private void button3_Click(object sender, EventArgs e)
  57.         {
  58.             SqlCommand procedureCall = new SqlCommand("dbo.averageOfId", con);
  59.             procedureCall.CommandType = CommandType.StoredProcedure;
  60.             con.Open();
  61.  
  62.             Object output = procedureCall.ExecuteScalar();
  63.  
  64.             MessageBox.Show(output.ToString());
  65.             con.Close();
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment