ADO.net通过存储过程,实现增删查改

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _13_用存储过程实现增删查改
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //增加
        private void button1_Click(object sender, EventArgs e)
        {
            //拿数据
            
                string name = textBoxname.Text.Trim();
                int age = Convert.ToInt32(textBoxage.Text.Trim());
                int height = Convert.ToInt32(textBox3height.Text.Trim());
                int gender = Convert.ToInt16(textBox4gender.Text.Trim());
          
            string sql = "usp_insertStudent";
            SqlParameter[] pms = new SqlParameter[]
                {
                    new SqlParameter("@name",SqlDbType.NVarChar,32) { Value = name},
                    new SqlParameter("@age",SqlDbType.Int) { Value = age},
                    new SqlParameter("@height",SqlDbType.Int) { Value = height},
                    new SqlParameter("@Gender",SqlDbType.SmallInt) { Value = gender},
                };
            int r = _07城市列表和递归Treeview.SqlHelper.ExecuteNonQuery(sql, CommandType.StoredProcedure, pms);
            if (r!=0)
            {
                MessageBox.Show("插入成功");
            }
            ReloadData();



        }
        
        private void button5_Click(object sender, EventArgs e)
        {
            ReloadData();
        }
        //查询
        private void ReloadData()
        {
            string sql = "usp_selectStudent";
            DataTable dt = _07城市列表和递归Treeview.SqlHelper.SqlDataAdapter(sql, CommandType.StoredProcedure);
            dataGridView1.DataSource = dt;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ReloadData();
        }
        //删除
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel);
            if (dr==DialogResult.OK)
            {
                string sql = "usp_delStudent";
                SqlParameter[] pms = new SqlParameter[]
                {
                new SqlParameter("@id",SqlDbType.Int) { Value=Convert.ToInt32( textBoxID.Text.Trim())}
                };
                int r = _07城市列表和递归Treeview.SqlHelper.ExecuteNonQuery(sql, CommandType.StoredProcedure, pms);
                if (r > 0)
                {
                    MessageBox.Show("删除成功!");
                }
                ReloadData();

            }
           
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ReloadData();
        }
        //修改
        private void button4_Click(object sender, EventArgs e)
        {
            string name = textBoxname.Text.Trim();
            int age = Convert.ToInt32(textBoxage.Text.Trim());
            int height = Convert.ToInt32(textBox3height.Text.Trim());
            int gender = Convert.ToInt16(textBox4gender.Text.Trim());
            int id = Convert.ToInt32(textBoxID.Text.Trim());

            string sql = "usp_updateStudent";
            SqlParameter[] pms = new SqlParameter[]
                {
                    new SqlParameter("@name",SqlDbType.NVarChar,32) { Value = name},
                    new SqlParameter("@age",SqlDbType.Int) { Value = age},
                    new SqlParameter("@height",SqlDbType.Int) { Value = height},
                    new SqlParameter("@Gender",SqlDbType.SmallInt) { Value = gender},
                    new SqlParameter("@id",SqlDbType.Int) { Value=id}
                };
            int r= _07城市列表和递归Treeview.SqlHelper.ExecuteNonQuery(sql, CommandType.StoredProcedure, pms);
            if (r>0)
            {
                MessageBox.Show("修改成功");
            }
        }
    }
}
use SqlDemos
go

select * from Student

--用存储过程实现增删查改

--1.增加数据
go
create proc usp_insertStudent
	@name  nvarchar(32)
	,@age int 
	,@height int
	,@Gender smallint
	as
	begin
		insert into Student values (@name,@age,@height,@Gender)
	end

go
create proc usp_delStudent
	@id int
	as
	begin
		delete from Student where stId=@id
	end

go
create proc usp_selectStudent
	as
	begin
		select * from Student
	end
go
create proc usp_updateStudent
	@name  nvarchar(32)
	,@age int 
	,@height int
	,@Gender smallint
	,@id int
	as
	begin
	 update Student set stName=@name,stAge=@age,stHeight=@height,stGender=@Gender where stId=@id
	end
	go
exec usp_updateStudent '韦小宝',18,180,1,5

你可能感兴趣的:(C#,ADO.net,SQL学习笔记,T-SQL编程)