vakho

[ADO.NET] Procedures & Parameters

Jan 16th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. /*
  2.  
  3. ALTER PROCEDURE updateSalary
  4.     @id INT,
  5.     @newSalary INT,
  6.     @salary INT OUTPUT
  7. AS
  8. BEGIN
  9.     UPDATE dbo.Users
  10.     SET @salary=salary+@newSalary, salary=salary+@newSalary
  11.     WHERE id=@id;
  12. END
  13.  
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.ComponentModel;
  18. using System.Data;
  19. using System.Drawing;
  20. using System.Linq;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. using System.Windows.Forms;
  24. using System.Data.SqlClient;
  25.  
  26. namespace WindowsFormsApplication1
  27. {
  28.     public partial class Form1 : Form
  29.     {
  30.         private string connectionString = "Server=VAKHO-PC; Database=TestDB; Integrated Security=true;";
  31.         private SqlConnection con;
  32.         private SqlDataAdapter adapter;
  33.         private DataSet dataSet = null;
  34.  
  35.         public Form1()
  36.         {
  37.             InitializeComponent();
  38.         }
  39.  
  40.         private void refreshTable()
  41.         {
  42.             this.dataSet = new DataSet("users");
  43.  
  44.             adapter.Fill(dataSet, "users");
  45.             dataGridView1.DataSource = dataSet.Tables[0];
  46.         }
  47.  
  48.         private void Form1_Load(object sender, EventArgs e)
  49.         {
  50.             this.con = new SqlConnection(this.connectionString);
  51.             this.adapter = new SqlDataAdapter("SELECT * FROM dbo.Users", con);
  52.  
  53.             this.refreshTable();
  54.  
  55.             try
  56.             {
  57.                 con.Open();
  58.             }
  59.             catch (SqlException ex)
  60.             {
  61.                 MessageBox.Show(ex.Message, "Sql Exception!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  62.             }
  63.             finally
  64.             {
  65.                 MessageBox.Show(con.State.ToString(), "Sql Exception!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  66.                 con.Close();
  67.             }
  68.         }
  69.  
  70.         private void button1_Click(object sender, EventArgs e)
  71.         {
  72.             this.refreshTable();
  73.         }
  74.  
  75.         private void button2_Click(object sender, EventArgs e)
  76.         {
  77.             SqlCommand updateCommand = new SqlCommand("dbo.updateSalary", con);
  78.             updateCommand.CommandType = CommandType.StoredProcedure;
  79.  
  80.             SqlParameter id = new SqlParameter("@id", SqlDbType.Int);
  81.             SqlParameter newSalary = new SqlParameter("@newSalary", SqlDbType.Int);
  82.             SqlParameter salary = new SqlParameter("@salary", SqlDbType.Int);
  83.  
  84.             updateCommand.Parameters.Add(id).Value = Convert.ToDecimal(textBox1.Text);
  85.             updateCommand.Parameters.Add(newSalary).Value = Convert.ToDecimal(textBox2.Text);
  86.             updateCommand.Parameters.Add(salary).Direction = ParameterDirection.Output;
  87.  
  88.             try
  89.             {
  90.                 con.Open();
  91.                 updateCommand.ExecuteNonQuery();
  92.                 MessageBox.Show(salary.Value.ToString(), "Updated Salary", MessageBoxButtons.OK, MessageBoxIcon.Information);
  93.             }
  94.             catch (SqlException ex)
  95.             {
  96.                 MessageBox.Show(ex.Message, "Sql Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  97.             }
  98.             finally
  99.             {
  100.                 con.Close();
  101.             }
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment