数据库--存储过程
时间:2011-02-27 来源:双魂人生
其实这些东西都是死的,多写几遍就ok了
A、不带参数的存储过程
create proc stuInfo
as
select * from student
go
B、带参数的存储过程
create proc stuBysex
@sex varchar(50)
as
select * from student where sex=@sex
go
C、带输出参数的存储过程
create proc stuOutNamebyid
@outName varchar(50) output
as
select @outName=name from student where id=2
go
declare @outName varchar(50)
exec stuOutNamebyid @outName output
print @outName
下面是增删改的存储过程
增加
create proc insertStu
@name varchar(50),
@sex varchar(50),
@age int,
@address varchar(50)
as
insert into Student(name,sex,age,address)values(@name,@sex,@age,@address)
go
exec insertStu 'shuang','女',34,'aaa'
删除
create proc delStu
@id int
as
delete from Student where id=@Id
go
exec delStu '5'
修改
create proc UpdateStu
@id int
as
update Student set name='aa' where id=@id
go
exec UpdateStu '4'
上面就是我们创建存储过程的写法,那么在程序中怎么使用,其实在不同的语言中各有个的调用方法,现在看看在.Net中怎么使用,看下面的例子
public static IList<FileInfor> GetFile(int id)
{
using(OleDbConnection connecting=new OleDbConnection(Connstr))
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connecting;
cmd.CommandText="存储过程名称";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OleDbParameter("@id",id));
connecting.Open();
using(OleDbDataReader reader=c)
{
.......
}
}
}
最重要的是 cmd.CommandType = CommandType.StoredProcedure;默认的情况下是调用sql语句的时候cmd.CommandType = CommandType.Text,所以记得把这个地方改一下就行了,其他的参数该怎么传,还怎么传,但是要保证参数的名称要保持一致...