zoukankan      html  css  js  c++  java
  • SQL语句中output的用法

    SQL语句中,output可以作为返回值来使用,

    1.我们先看这个存储过程

    代码:

    1 set ANSI_NULLS ON
     2 set QUOTED_IDENTIFIER ON
     3 go
     4
     5 --added by hendyzhu  验证身份证的唯一性,排除同一个人有多个居民健康档案
     6 --2010-5-28
     7 create procedure [dbo].[DistinctSame]
     8 (
     9 @ID varchar(20),
    10 @count int output --这里我们看到设置@count为output,即为输出型的参数
    11 )
    12 as
    13 begin
    14 select @count=count(*) from MainArchives where ID=@ID
    15 end
    16
     
    执行完这个存储过程之后,@count将作为该存储过程的返回值被返回。

    2.现在在C#代码中,我们就可以新建一个输出型参数,当数据库执行完查询之后,我们就可以通过getParameterValue()方法获取到存储过程的返回值。

    代码:

    1 public static bool IsNotRegistered(string id)
     2         {
     3             Database db = DatabaseFactory.CreateDatabase();
     4             DbCommand creditCommand =db.GetStoredProcCommand("dbo.DistinctSame");
     5             db.AddInParameter(creditCommand, "@ID", DbType.StringFixedLength,id.Trim());
     6             db.AddOutParameter(creditCommand,"@count",DbType.StringFixedLength,32767);  //注意到这里,给存储过程添加了一个@count输出型参数
     7             db.ExecuteNonQuery(creditCommand);//执行查询
     8             int result=int.Parse(db.GetParameterValue(creditCommand,"@count").ToString());//获取存储过程的返回值
     9             if (result == 0)
    10             {
    11                 return true;
    12             }
    13             else
    14             {
    15                 return false;
    16             }
    17         }

  • 相关阅读:
    实现自己的Linq to Sql
    [分享] 浅谈项目需求变更管理
    【分享】老程序员的经验和价值在哪里?
    程序员是自己心中的“上帝”
    [分享]解析“程序员的十大技术烦恼”
    【分享】帮助你早些明白一些道理
    “风雨20年”的20条精辟编程经验
    【分享】 优秀程序员的代码是“活的”
    给开发人员培训时的语录
    【分享】SQL Server优化50法
  • 原文地址:https://www.cnblogs.com/SamllBaby/p/3512210.html
Copyright © 2011-2022 走看看