Advertisement
vakho

ADO.NET Procedures!

Nov 22nd, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. /* procedure
  2. alter procedure dbo.UpdateSalary
  3.     @id int,
  4.     @changeSalary decimal(6, 1),
  5.     @salary decimal(6, 1) output
  6. as
  7. begin
  8.     update Users
  9.     set salary=@changeSalary
  10.     where id = @id
  11. end
  12. */
  13.  
  14. using System;
  15. using System.Collections.Generic;
  16. using System.ComponentModel;
  17. using System.Data;
  18. using System.Drawing;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Windows.Forms;
  22. using System.Data.SqlClient;
  23.  
  24. namespace AdoApp
  25. {
  26.     public partial class Form1 : Form
  27.     {
  28.         string connectionString = @"Server=LAB-418-10-PC\STUDENT; Database=vakho; Integrated Security=true;";
  29.         SqlConnection con;
  30.  
  31.         public Form1()
  32.         {
  33.             InitializeComponent();
  34.             this.con = new SqlConnection(this.connectionString);
  35.         }
  36.  
  37.         private void button1_Click(object sender, EventArgs e)
  38.         {
  39.             try
  40.             {
  41.                 SqlCommand insertCommand = new SqlCommand("INSERT INTO dbo.users (name, surname, birthday, salary) VALUES (@name, @surname, @birthday, @salary)", con);
  42.  
  43.                 SqlParameter name = new SqlParameter("@name", DbType.String);
  44.                 SqlParameter surname = new SqlParameter("@surname", DbType.String);
  45.                 SqlParameter birthday = new SqlParameter("@birthday", SqlDbType.Date);
  46.                 SqlParameter salary = new SqlParameter("@salary", SqlDbType.Decimal);
  47.  
  48.                 insertCommand.Parameters.Add(name).Value = textBox1.Text;
  49.                 insertCommand.Parameters.Add(surname).Value = textBox2.Text;
  50.                 insertCommand.Parameters.Add(birthday).Value = dateTimePicker1.Value.ToShortDateString();
  51.                 insertCommand.Parameters.Add(salary).Value = Convert.ToDecimal(textBox3.Text);
  52.  
  53.                 con.Open();
  54.                 insertCommand.ExecuteNonQuery();
  55.             }
  56.             catch (SqlException ex)
  57.             {
  58.                 MessageBox.Show(ex.Message, "Sql Error!");
  59.             }
  60.             catch (FormatException ex)
  61.             {
  62.                 MessageBox.Show(ex.Message, "Format Error!");
  63.             }
  64.             finally
  65.             {
  66.                 con.Close();
  67.             }
  68.         }
  69.  
  70.         private void button2_Click(object sender, EventArgs e)
  71.         {
  72.             try
  73.             {
  74.                 SqlCommand updateCommand = new SqlCommand("dbo.UpdateSalary @id, @changeSalary, @salary", con);
  75.  
  76.                 SqlParameter id = new SqlParameter("@id", SqlDbType.Int);
  77.                 SqlParameter newSalary = new SqlParameter("@changeSalary", SqlDbType.Decimal);
  78.                 SqlParameter salary = new SqlParameter("@salary", SqlDbType.Decimal);
  79.  
  80.                 updateCommand.Parameters.Add(id).Value = textBox4.Text;
  81.                 updateCommand.Parameters.Add(newSalary).Value = Convert.ToDecimal(textBox3.Text);
  82.                 updateCommand.Parameters.Add(salary).Direction = ParameterDirection.Output;
  83.  
  84.                 con.Open();
  85.                 updateCommand.ExecuteNonQuery();
  86.                 // parameter not showing properly!
  87.                 MessageBox.Show(updateCommand.Parameters[updateCommand.Parameters.Count-1].Value.ToString(), "New Salary!");
  88.             }
  89.             catch (SqlException ex)
  90.             {
  91.                 MessageBox.Show(ex.Message, "Sql Error!");
  92.             }
  93.             catch (FormatException ex)
  94.             {
  95.                 MessageBox.Show(ex.Message, "Format Error!");
  96.             }
  97.             finally
  98.             {
  99.                 con.Close();
  100.             }
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement