Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //alter procedure updateSalary
- // @id int,
- // @newSalary decimal(6,1),
- // @salary decimal(6,1) output
- //as
- //begin
- // update Users set @salary=salary+@newSalary, salary=salary+@newSalary where id=@id;
- //end
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace UpdateSalary
- {
- public partial class Form1 : Form
- {
- SqlConnection con;
- public Form1()
- {
- con = new SqlConnection(@"Server=VAKHO-PC; Database=TestDB; Integrated Security=true;");
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- SqlCommand updateCommand = new SqlCommand("dbo.updateSalary", con);
- updateCommand.CommandType = CommandType.StoredProcedure;
- SqlParameter id = new SqlParameter("@id", SqlDbType.Int);
- SqlParameter newSalary = new SqlParameter("@newSalary", SqlDbType.Decimal);
- SqlParameter salary = new SqlParameter("@salary", SqlDbType.Decimal);
- updateCommand.Parameters.Add(id).Value = Convert.ToDecimal(textBox1.Text);
- updateCommand.Parameters.Add(newSalary).Value = Convert.ToDecimal(textBox2.Text);
- updateCommand.Parameters.Add(salary).Direction = ParameterDirection.Output;
- try
- {
- con.Open();
- updateCommand.ExecuteNonQuery();
- MessageBox.Show(salary.Value.ToString(), "Updated Salary", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (SqlException ex)
- {
- MessageBox.Show(ex.Message, "Sql Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- con.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment