Thursday, March 15, 2012

Stored procedures


Insert:
create procedure spinsert(@UserId nvarchar(50),@UserName nvarchar(50)=null)
as
begin
insert into proc1 values(@UserId,@UserName)
end


delete:
create proc spdelete(@UserId nvarchar(50)=null)
as
begin
delete from proc1 where UserId=@UserId
end


Update:

create proc spupdate(@UserId nvarchar(50)=null,@UserName nvarchar(50)=null)
as
begin
update proc1 set UserId=@UserId,UserName=@UserName where UserId=@UserId
end
select:
create proc spselect(@UserId nvarchar(50)=null,@UserName nvarchar(50)=null)
as
begin
select UserName from proc1 where UserId=@UserId
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 stp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection("uid=sa;password=123;database=naseer");

      

 private void insert_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("spinsert",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserId",textBox1.Text);
            cmd.Parameters.AddWithValue("@UserName",textBox2.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("inserted succesfully");
        }

      

 private void delete_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("spdelete", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserId", textBox1.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("deleted succesfully");
        }

      


 private void update_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("spupdate", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserId", textBox1.Text);
            cmd.Parameters.AddWithValue("@UserName", textBox2.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("updated succesfully");
        }


     
 private void select_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("spselect", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserId", textBox1.Text);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                textBox2.Text = dt.Rows[0]["UserName"].ToString();
            }
            else
            {
                MessageBox.Show("Please enter Correct UserID");
            }
            
         
        }

    }
}



Thanks&Regards,
Naseer.

No comments:

Post a Comment