SQL output使用
2009-11-20 17:35
429 查看
一直没有找到一种好的方法来引用有返回值的存储过程的方法,使我在添加数据中走了不少的弯路,最近,在查阅了大量的资料之后,终于在微软的一个实例中找到了一种良好的方法。
首先编写好一有返回值的存储过程
create procedure proc_name
@para1 nchar(20), --输入参数
@para2 int = null out --输出参数,供程序使用
as
set nocount on
if ( not exists (select * from employee where em_name=@para1))
begin
insert into employee(name) values(@para1)
select @para2=@@identity --返回添加记录的ID
return 1 --返回是否成功添加数据
end
else
return 0 --返回失败
go
然后是调用存储过程的方法
sqlcommand command;
command = new sqlcommand(proc_name,new sqlconnection(connectionstr));
command.paraments.add("@para1"),"name1"); //输入参数,职员姓名
command.paraments.add(new sqlparament("@para2", //生成一输出参数
SqlDbType.Int; //参数数据类型
ParamenterDirection.OutPut, //输入输出类型
0,
0,
string.Emplty,
DataRowVerstion.Default,
null) //参数值,输入参数时需提供
);
command.commandtype=commandtype.StoredProcedure;
command.connection.open();
command.executenonQuery();
int pkid=(int)command.Parameters["@para2"].value; //得到输出参数的值
command.connection.close();
此处是引用输出参数,如果要引用返回值(是否成功添加数据)则只需把ParamenterDirection的类型改为returnvalue;再自己改一个参数名就可以了.
首先编写好一有返回值的存储过程
create procedure proc_name
@para1 nchar(20), --输入参数
@para2 int = null out --输出参数,供程序使用
as
set nocount on
if ( not exists (select * from employee where em_name=@para1))
begin
insert into employee(name) values(@para1)
select @para2=@@identity --返回添加记录的ID
return 1 --返回是否成功添加数据
end
else
return 0 --返回失败
go
然后是调用存储过程的方法
sqlcommand command;
command = new sqlcommand(proc_name,new sqlconnection(connectionstr));
command.paraments.add("@para1"),"name1"); //输入参数,职员姓名
command.paraments.add(new sqlparament("@para2", //生成一输出参数
SqlDbType.Int; //参数数据类型
ParamenterDirection.OutPut, //输入输出类型
0,
0,
string.Emplty,
DataRowVerstion.Default,
null) //参数值,输入参数时需提供
);
command.commandtype=commandtype.StoredProcedure;
command.connection.open();
command.executenonQuery();
int pkid=(int)command.Parameters["@para2"].value; //得到输出参数的值
command.connection.close();
此处是引用输出参数,如果要引用返回值(是否成功添加数据)则只需把ParamenterDirection的类型改为returnvalue;再自己改一个参数名就可以了.
相关文章推荐
- sql存储过程中使用 output
- SQL 语句中output参数的使用 很实用!
- sql 中使用了output类型,那么在程序中如何传这个参数呢
- sql output参数 Sql Output 参数和asp.net的sqlparameter配合使用
- 使用PL/SQL Developer的Event Monitor代替Output做调试。
- sql 2005中sp_executesql系统存储过程带输出参数 OUTPUT 的使用说明
- 使用PL/SQL Developer的Event Monitor代替Output做调试。
- 使用SqlDataReader应该注意的问题,使用datareader没有获取到分页的output值【来自Mist .NET blog】
- 在修改数据的SQL语句中使用OUTPUT子句带来的好处
- sql 2008 使用output避免数据修改后的二次查询
- 使用C语言访问MySQL数据 —— 执行SQL语句(2)
- [SQL使用经验]选用 IN 还是 EXISTS,哪个性能更好
- 数据库 SQL 语句 去除字段中空格,换行符,回车符(使用replace语句)
- 使用Explain分析SQL
- PL/SQL Developer使用技巧、快捷键
- SQL游标原理和使用方法
- PL/SQL Developer配置使用手册(整理)
- MySQL ProxySQL读写分离使用初探
- sql server cast 和 convert函数使用
- 本机不安装Oracle客户端,使用PL/SQL Developer连接远程数据库