有些初学者在调用存储过程时容易出现一些错误,下面我就不同的返回参数而言举个用户登录常用的例子做一些简单的讨论:(由简单--->>深入)
创建数据库:(Student)
创建用户表:(Users)
use Student
go
create table Users
(
id int identity(1, 1) primary key,
name nvarchar(20) not null,
password nvarchar(20)
)
1.带输入参数的存储过程
create procedure proc_login
as
begin
select *
from Users
where name = @name and password = @password
end
go
调用带输入参数的存储过程
//连接数据库的字符串
private string connectionString = ConfigurationManager.AppSetting["connectionString"];
//数据层的登录方法
public static User Login(string name, string password) {
User user = null;
using (SqlConnection conn = new SqlConnection(connectionString)) {
}
return user;
}
2.带输入输出参数的存储过程
create procedure proc_login
@message nvarchar(50) output //用于输出用户登录的信息
as
begin
//判断用户名是否存在
if exists (select * from Users where name = @name)
begin
end
else
begin
end
//在过程中如果有错误
if @@error <> 0
begin
end
end
go
调用带输入输出参数的存储过程
//数据层的登录方法
public static User Login(string name, string password, out string message) {
User user = null;
using (SqlConnection conn = new SqlConnection(connectionString)) {
}
return user;
}
3.带返回值得存储过程
create procedure proc_login
as
declare @message nvarchar(50) output //用于返回用户登录的信息
begin
//判断用户名是否存在
if exists (select * from Users where name = @name)
begin
end
else
begin
end
//在过程中如果有错误
if @@error <> 0
begin
end
return @message
end
go
调用带返回值得存储过程
//数据层的登录方法
public static User Login(string name, string password, out string message) {
User user = null;
using (SqlConnection conn = new SqlConnection(connectionString)) {
}
return user;
}