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
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