一、添加存储过程如下:
create procedure Addemp
@emp_login varchar(50),
@password varchar(50),
@emp_name varchar(50),
@duty varchar(50),
@email varchar(50),
@mobile_phone varchar(50),
@work_phone varchar(50),
@jb int
as
insert into emp
(
emp_login,
password,
emp_name,
duty,
email,
mobile_phone,
work_phone,
jb
)
values
(
@emp_login,
@password,
@emp_name,
@duty,
@email,
@mobile_phone,
@work_phone,
@jb
)
go
添加代码生成
private void Btn_ok_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
SqlCommand cm=new SqlCommand("AddEmp",cn);//调用存储过程
cm.CommandType=CommandType.StoredProcedure;//类型转换
参数的调用
cm.Parameters.Add(new SqlParameter("@Emp_login",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@password",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@name",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@duty",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Email",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@mobile_tell",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@work_tell",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@jb",SqlDbType.Int,4));
参数赋值
cm.Parameters["@Emp_login"].Value=Tbx_id.Text;
cm.Parameters["@password"].Value=Tbx_id.Text;
cm.Parameters["@name"].Value=Tbx_name.Text;
cm.Parameters["@duty"].Value=duty.SelectedItem.Value;
cm.Parameters["@Email"].Value=Tbx_Email.Text;
cm.Parameters["@mobile_tell"].Value=mobile_tell.Text;
cm.Parameters["@work_tell"].Value=work_tell.Text;
cm.Parameters["@jb"].Value=jb.SelectedItem.Value;
数据更新命令的执行
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();//不返回值
Response.Redirect("Emp.aspx");
}
catch(SqlException)
{
Lbl_note.Text="添加失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
}
}
private void Btn_cancel_Click(object sender, System.EventArgs e)
{
Page.Response.Redirect("addemp.aspx");
}
private void Cv_id_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
cn.Open();
SqlCommand cm=new SqlCommand("select * from Emp where emp_login=@emp_login",cn);
cm.Parameters.Add("@emp_login",SqlDbType.Char,10);
cm.Parameters["@emp_login"].Value=Tbx_id.Text;
SqlDataReader dr=cm.ExecuteReader();
if(dr.Read())
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
cn.Close();
}
二、数据更新
1,存储过程如下:
create procedure editemp
@duty varchar(50),
@email varchar(50),
@mobile_phone varchar(50),
@work_phone varchar(50),
@emp_id int
as
update emp
set
duty=@duty,
email=@email,
mobile_phone=@mobile_phone,
work_phone=@work_phone
where
emp_id=@emp_id
go
2、执行代码
private void Dgd_user_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlCommand cm=new SqlCommand("EditEmp",cn);//调用存储过程
cm.CommandType=CommandType.StoredProcedure;//类型转换
cm.Parameters.Add(new SqlParameter("@duty",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Email",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@mobile_tell",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@work_tell",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Emp_id",SqlDbType.Int,4));
string colvalue=((TextBox)e.Item.Cells[4].Controls[0]).Text;
cm.Parameters["@duty"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[5].Controls[0]).Text;
cm.Parameters["@Email"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[6].Controls[0]).Text;
cm.Parameters["@mobile_tell"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[7].Controls[0]).Text;
cm.Parameters["@work_tell"].Value=colvalue;
cm.Parameters["@Emp_id"].Value=Dgd_user.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="编辑成功";
Dgd_user.EditItemIndex=-1;
}
catch(SqlException)
{
Lbl_note.Text="编辑失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
BindGrid();
}
三、数据删除
1、存储过程
create procedure deleteemp
@emp_id int
as
delete
from
emp
where
emp_id=@emp_id
go
2、执行代码
private void Dgd_user_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlCommand cm=new SqlCommand("deleteEmp",cn);
cm.CommandType=CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@Emp_id",SqlDbType.Int,4));
cm.Parameters["@Emp_id"].Value=Dgd_user.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="删除成功";
}
catch(SqlException)
{
Lbl_note.Text="删除失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
BindGrid();
}