vakho

ADO.NET UpdateSalary (Procedures)

Nov 28th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. //alter procedure updateSalary
  2. //    @id int,
  3. //    @newSalary decimal(6,1),
  4. //    @salary decimal(6,1) output
  5. //as
  6. //begin
  7. //    update Users set @salary=salary+@newSalary, salary=salary+@newSalary where id=@id;
  8. //end
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Data;
  14. using System.Drawing;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Windows.Forms;
  18. using System.Data.SqlClient;
  19.  
  20. namespace UpdateSalary
  21. {
  22.     public partial class Form1 : Form
  23.     {
  24.         SqlConnection con;
  25.  
  26.         public Form1()
  27.         {
  28.             con = new SqlConnection(@"Server=VAKHO-PC; Database=TestDB; Integrated Security=true;");
  29.             InitializeComponent();
  30.         }
  31.  
  32.         private void button1_Click(object sender, EventArgs e)
  33.         {
  34.             SqlCommand updateCommand = new SqlCommand("dbo.updateSalary", con);
  35.             updateCommand.CommandType = CommandType.StoredProcedure;
  36.  
  37.             SqlParameter id = new SqlParameter("@id", SqlDbType.Int);
  38.             SqlParameter newSalary = new SqlParameter("@newSalary", SqlDbType.Decimal);
  39.             SqlParameter salary = new SqlParameter("@salary", SqlDbType.Decimal);
  40.  
  41.             updateCommand.Parameters.Add(id).Value = Convert.ToDecimal(textBox1.Text);
  42.             updateCommand.Parameters.Add(newSalary).Value = Convert.ToDecimal(textBox2.Text);
  43.             updateCommand.Parameters.Add(salary).Direction = ParameterDirection.Output;
  44.  
  45.             try
  46.             {
  47.                 con.Open();
  48.                 updateCommand.ExecuteNonQuery();
  49.                 MessageBox.Show(salary.Value.ToString(), "Updated Salary", MessageBoxButtons.OK, MessageBoxIcon.Information);
  50.             }
  51.             catch (SqlException ex)
  52.             {
  53.                 MessageBox.Show(ex.Message, "Sql Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  54.             }
  55.             finally
  56.             {
  57.                 con.Close();
  58.             }
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment