--登录存储过程
create proc [dbo].[dengru]
@zhanghoa nvarchar(10),
@mima nvarchar(10),
@num int output--返回参数的作用是为了给后台返回结果
as
if exists(select * from Students a where a.StudentName=@zhanghoa and a.Password=@mima)
begin
set @num=1--代表登录成功
end
else
begin
set @num=0--登录失败
end
EF调用存储过程的方式:
/// <summary>
/// 登录存储过程
/// </summary>
/// <param name="name"></param>
/// <param name="pwd"></param>
/// <returns></returns>
public int Login(string name,string pwd)
{
DataContext contexct = new DataContext();
var _name = new SqlParameter("@zhanghoa", name);//用户名
var _pwd = new SqlParameter("@mima", pwd);//密码
var _out = new SqlParameter("@num", SqlDbType.Int);//返回值,用于判断用户是否存在
_out.Direction = ParameterDirection.Output;//定义该参数为返回参数
contexct.Database.ExecuteSqlCommand("login @zhanghoa,@mima,@num out", _name, _pwd,_out);
int result = (int)_out.Value;//获取返回参数的值
return result;
}
/// <summary>
/// 添加学生信息
/// </summary>
/// <returns></returns>
public int AddStudent(Student stu)
{
try
{
DataContext context = new DataContext();
var st_name = new SqlParameter("@name", stu.StudentName);
var st_password = new SqlParameter("@pasword", stu.PassWord);
var st_sex = new SqlParameter("@sex", stu.Sex);
var st_deptid = new SqlParameter("@deptid", stu.DeptId);
var st_state = new SqlParameter("@state", stu.State);
return context.Database.ExecuteSqlCommand("add_stu @name,@sex,@pasword,@deptid,@state", st_name, st_sex, st_password, st_deptid,st_state);
}
finally
{
GC.Collect();
}
}
/// <summary>
/// 删除存储过程
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public int DelStu(int id)
{
DataContext context = new DataContext();
var stu = context.stu.Find(id);//根据编号查找学生信息
if(stu.State==1)
{
return 2;//用户不能删除,当前的状态是正常
}
else
{
try
{
var p_name = new SqlParameter("@id", id);
return context.Database.ExecuteSqlCommand("del_stu @id", p_name);//执行删除的存储过程
}
finally
{
GC.Collect();
}
}
}