create table Users ( id int identity(1, 1) primary key, name nvarchar(20) not null, passWord nvarchar(20) not null ) use Student go
create table Users ( id int identity(1, 1) primary key, name nvarchar(20) not null, password nvarchar(20) not null )
1.带输入参数的存储过程 view plaincopy to clipboardprint? create procedure proc_login @name nvarchar(20), @password nvarchar(20) as begin select * from Users where name = @name and password = @password end go create procedure proc_login @name nvarchar(20), @password nvarchar(20) as begin select * from Users where name = @name and password = @password end go
2.带输入输出参数的存储过程 view plaincopy to clipboardprint? create procedure proc_login @name nvarchar(20), @password nvarchar(20), @message nvarchar(50) output //用于输出用户登录的信息 as begin //判断用户名是否存在 if exists (select * from Users where name = @name) begin //判断密码是否正确 select * from Users where name = @name and password = @password //返回的行数〉0 if @@rowcount > 0 begin set @message = '登录成功!' end else begin set @message = '密码错误!' end end else begin set @message = '用户名不存在!' end //在过程中如果有错误 if @@error <> 0 begin set @message = '登录失败!' end end go create procedure proc_login @name nvarchar(20), @password nvarchar(20), @message nvarchar(50) output //用于输出用户登录的信息 as begin //判断用户名是否存在 if exists (select * from Users where name = @name) begin //判断密码是否正确 select * from Users where name = @name and password = @password //返回的行数〉0 if @@rowcount > 0 begin set @message = '登录成功!' end else begin set @message = '密码错误!' end end else begin set @message = '用户名不存在!' end //在过程中如果有错误 if @@error <> 0 begin set @message = '登录失败!' end end go
调用带输入输出参数的存储过程
view plaincopy to clipboardprint? //数据层的登录方法 public static User Login(string name, string password, out string message) { User user = null; using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand("proc_login", conn); cmd.CommandType = CommandType.StoredProcedure; //指定调用存储过程
3.带返回值得存储过程 view plaincopy to clipboardprint? create procedure proc_login @name nvarchar(20), @password nvarchar(20) as declare @message nvarchar(50) output //用于返回用户登录的信息 begin //判断用户名是否存在 if exists (select * from Users where name = @name) begin //判断密码是否正确 select * from Users where name = @name and password = @password //返回的行数〉0 if @@rowcount > 0 begin set @message = '登录成功!' end else begin set @message = '密码错误!' end end else begin set @message = '用户名不存在!' end //在过程中如果有错误 if @@error <> 0 begin set @message = '登录失败!' end return @message end go create procedure proc_login @name nvarchar(20), @password nvarchar(20) as declare @message nvarchar(50) output //用于返回用户登录的信息 begin //判断用户名是否存在 if exists (select * from Users where name = @name) begin //判断密码是否正确 select * from Users where name = @name and password = @password //返回的行数〉0 if @@rowcount > 0 begin set @message = '登录成功!' end else begin set @message = '密码错误!' end end else begin set @message = '用户名不存在!' end //在过程中如果有错误 if @@error <> 0 begin set @message = '登录失败!' end return @message end go
调用带返回值得存储过程 view plaincopy to clipboardprint? //数据层的登录方法 public static User Login(string name, string password, out string message) { User user = null; using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand("proc_login", conn); cmd.CommandType = CommandType.StoredProcedure; //指定调用存储过程