zoukankan      html  css  js  c++  java
  • 在c#中的取得sql存储过程的output参数及自增列@@identity变量

    SQL output使用

    C#常用   2009-07-21 18:03   阅读269   评论2  
    字号:    

    一直没有找到一种好的方法来引用有返回值的存储过程的方法,使我在添加数据中走了不少的弯路,最近,在查阅了大量的资料之后,终于在微软的一个实例中找到了一种良好的方法。
    首先编写好一有返回值的存储过程
    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 Server 2005返回刚刚插入的数据条目id值

    @@identity系统变量

    转自http://hi.baidu.com/xiaoxiaolq/blog/item/b290410fc47c54206159f308.html

  • 相关阅读:
    2018年-2019年第二学期第七周C#学习个人总结
    2018年-2019年第二学期第六周C#学习个人总结
    2018年-2019年第二学期第五周C#学习个人总结
    2018年-2019年第二学期第四周C#学习个人总结
    2018年-2019年第二学期第三周C#学习个人总结
    2018年-2019年第二学期第二周C#学习个人总结
    本学期C#学习个人总结
    排球积分程序
    观后感
    最终总结
  • 原文地址:https://www.cnblogs.com/ostrich/p/1627284.html
Copyright © 2011-2022 走看看