Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* procedure
- alter procedure dbo.UpdateSalary
- @id int,
- @changeSalary decimal(6, 1),
- @salary decimal(6, 1) output
- as
- begin
- update Users
- set salary=@changeSalary
- 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 AdoApp
- {
- public partial class Form1 : Form
- {
- string connectionString = @"Server=LAB-418-10-PC\STUDENT; Database=vakho; Integrated Security=true;";
- SqlConnection con;
- public Form1()
- {
- InitializeComponent();
- this.con = new SqlConnection(this.connectionString);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- SqlCommand insertCommand = new SqlCommand("INSERT INTO dbo.users (name, surname, birthday, salary) VALUES (@name, @surname, @birthday, @salary)", con);
- SqlParameter name = new SqlParameter("@name", DbType.String);
- SqlParameter surname = new SqlParameter("@surname", DbType.String);
- SqlParameter birthday = new SqlParameter("@birthday", SqlDbType.Date);
- SqlParameter salary = new SqlParameter("@salary", SqlDbType.Decimal);
- insertCommand.Parameters.Add(name).Value = textBox1.Text;
- insertCommand.Parameters.Add(surname).Value = textBox2.Text;
- insertCommand.Parameters.Add(birthday).Value = dateTimePicker1.Value.ToShortDateString();
- insertCommand.Parameters.Add(salary).Value = Convert.ToDecimal(textBox3.Text);
- con.Open();
- insertCommand.ExecuteNonQuery();
- }
- catch (SqlException ex)
- {
- MessageBox.Show(ex.Message, "Sql Error!");
- }
- catch (FormatException ex)
- {
- MessageBox.Show(ex.Message, "Format Error!");
- }
- finally
- {
- con.Close();
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- SqlCommand updateCommand = new SqlCommand("dbo.UpdateSalary @id, @changeSalary, @salary", con);
- SqlParameter id = new SqlParameter("@id", SqlDbType.Int);
- SqlParameter newSalary = new SqlParameter("@changeSalary", SqlDbType.Decimal);
- SqlParameter salary = new SqlParameter("@salary", SqlDbType.Decimal);
- updateCommand.Parameters.Add(id).Value = textBox4.Text;
- updateCommand.Parameters.Add(newSalary).Value = Convert.ToDecimal(textBox3.Text);
- updateCommand.Parameters.Add(salary).Direction = ParameterDirection.Output;
- con.Open();
- updateCommand.ExecuteNonQuery();
- // parameter not showing properly!
- MessageBox.Show(updateCommand.Parameters[updateCommand.Parameters.Count-1].Value.ToString(), "New Salary!");
- }
- catch (SqlException ex)
- {
- MessageBox.Show(ex.Message, "Sql Error!");
- }
- catch (FormatException ex)
- {
- MessageBox.Show(ex.Message, "Format Error!");
- }
- finally
- {
- con.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement